Group UITableView for Selection
I h开发者_开发知识库ave a UITableView in Group Style, and I am using it with Navigation Controller. When a user click on the cell I am pushing to another view for a user to make selection, while all that is working fine. I want the user to be brought back to the first view when the user makes a selection. and I want to display their selection on the cell.
Thanks in advance for your help
I think you can use the NSUserDefaults.
// view controller to make selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selected;
if (indexPath.row == 0) {
selected = @"Apple";
} else if (indexPath.row == 1) {
selected = @"Microsoft";
}
[[NSUserDefaults standardUserDefaults] setObject:selected forKey:@"SELECTED"];
[self.navigationController popViewControllerAnimated:YES];
}
Important point is you need to call [self.tableView reloadData] in viewDidAppear.
// view controller to show what a user selected
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = @"Choice";
cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"SELECTED"];
return cell;
}
精彩评论