我有一個基於導航的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
感謝您的回覆。我想通過創建一個答案類並在用戶選擇下一個單元格上的答案時更新該類中的NSString。因爲我沒有將這些數據保存在設備上,所以我不認爲我需要使用Core數據。 – John 2011-02-28 03:21:02
嗨:如何清除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
當用戶按下按鈕時,我會調用你所做的方法,但我認爲你想通過做這樣的事情來抓住每個單元格:UITableViewCell * cell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection :0]];那麼你可以通過調用cell.detailTextLabel.text = @「」;來刪除這些單元格中的每個文本字段。 – tazboy 2011-02-28 16:04:39