2011-02-26 51 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