我有一个基于导航的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