2011-02-26 50 views
0

我有一个基于导航的UITableView应用程序。这是我的应用程序的工作原理。无法显示每个行的cell.detailtextlabel值

关于rootViewController我有一套问题,并在nextViewController(这也是UITableView)我有这些问题的多项选择答案。我希望用户点击这些问题并转到nextViewController选择答案并在rootViewController的cell.detailTextLabel中的问题下显示该答案。

我能够得到答案,但是当我将它显示在rootViewController上时,答案显示在所有行上。不知道如何做到这一点。

以下是我在rootViewController上的代码。我会很感激如果有人可以请帮助我。 谢谢!

#import "RootViewController.h" 
#import "NextViewController.h" 
#import "xxxAppDelegate.h" 

@implementation RootViewController 
@synthesize questions; 
@synthesize questAns; 
@synthesize aNote; 

#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.title = @"xxx"; 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Submit" style:UIBarButtonItemStyleBordered target:self action:@selector(SubmitBtn:)] autorelease]; 
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(ClearBtn:)] autorelease]; 
    questions = [[NSArray alloc] initWithObjects:@"Question 1", @"Question 2", @"Question 3", nil]; 

} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self.tableView reloadData]; 
} 
#pragma mark - 
#pragma mark Table view data source 

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [questions count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    cell.textLabel.text = [questions objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    xxxAppDelegate *mainDelegate = (xxxAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    cell.detailTextLabel.text = mainDelegate.MainLocLbl; 

    NSLog(@"Indexpath.row on root %d", indexPath.row); 
    NSLog(@"detail text %@", mainDelegate.MainLocLbl); 

    return cell; 
} 

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NextViewController *detailViewController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    //questAns = [questions objectAtIndex:indexPath.row]; 
    //detailViewController.answers = questAns; 
    xxxAppDelegate *mainDelegate = (xxxAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    detailViewController.aNote = mainDelegate.MainLocLbl; 
    NSLog(@"dtl ans %@", detailViewController.aNote); 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 

} 

#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 

回答

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

上述方法是配置每一个细胞。当您在应用程序代理中存储一个答案时,每个单元格都会得到相同的答案。

你需要做的是创建字典或其他方式存储数据在你的rootViewController。把答案放在那里,以便你存储所有的答案,而不仅仅是一个。

0

而不是制作一个字符串数组,我会考虑制作一个对象数组。每个对象都有一个NSString *问题,一个NSArray *答案,它将填充NSString答案,一个NSString *标题和一个NSString *副标题。

然后,当用户选择其中一个行时,您可以将该对象传递给下一个tableview。在下一个tableview中,当一个人选择其中一个答案时,它会将该对象的副标题设置为该行的标签。

我意识到你可能希望这是一个简单的项目,但我会为此使用核心数据。这可能是矫枉过正,但它使事情变得更加容易,特别是如果你想稍后变得更复杂的话。

+0

感谢您的回复。我想通过创建一个答案类并在用户选择下一个单元格上的答案时更新该类中的NSString。因为我没有将这些数据保存在设备上,所以我不认为我需要使用Core数据。 – John 2011-02-28 03:21:02

+0

嗨:如何清除UITableView cell.detailTextLabel.text中的内容?一旦用户点击顶部的左侧栏按钮,我想要清除所有答案?以下是我的代码。不知道这是否是正确的方法。 - (void)ClearBtn:(id)发送者静态NSString * CellIdentifier = @“Cell”; UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.detailTextLabel.text = @“test”; \t // NSLog(@“cell%@”,cell.detailTextLabel.text); [self.tableView reloadData]; \t } – John 2011-02-28 03:33:13

+0

当用户按下按钮时,我会调用你所做的方法,但我认为你想通过做这样的事情来抓住每个单元格:UITableViewCell * cell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection :0]];那么你可以通过调用cell.detailTextLabel.text = @“”;来删除这些单元格中的每个文本字段。 – tazboy 2011-02-28 16:04:39