我有一个表格视图,其中包含 AppDelegate 中的项目列表。表格视图允许用户选择他们想要的项目,并且在选择之后项目旁边有一个复选标记。接下来,用户可以返回上一个屏幕,此选择会更新显示给用户的字符串值。该值也存储在 AppDelegate 中。一切正常,但如果用户选择返回到用户可以更改其选择的屏幕,我希望自动选择此值。
这是我的 SetSizeViewController.m 中的 cellFroRowAtIndexPath 函数
我无法选择存储在 appDelegate.selectedSize 中的值。例如,此值可能类似于“Meter”。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.checkedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *result = nil;
static NSString *CellIdentifier = @"CellIdentifier";
result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];
SetSize *setSize = (SetSize *)[appDelegate.setSizes objectAtIndex:indexPath.row];
appDelegate.selectedSize = setSize.name;
appDelegate.selectedSizeCheckedIndexValue = (NSInteger *)indexPath.row;
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.selectedSize.length > 0)
{
if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {
[cell setSelected:YES animated:YES];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
[cell setSelected:NO];
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}