我有一个表格 View ,其中包含 AppDelegate 中的项目列表。表格 View 允许用户选择他们想要的项目,并且在选择后该项目旁边有一个复选标记。接下来,用户可以返回到上一屏幕,并且该选择更新显示给用户的字符串值。该值也存储在 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;
}
}
}
最佳答案
您应该避免自己调用didSelectRowAtIndexPath:
。无论如何,你应该尝试调用[result setSelected:YESanimated:YES]
。
另外,您如何检查该单元格是否是要选择的单元格?如果我错了,请纠正我,但在我看来,使用这个 if(appDelegate.selectedSize) { ...
,每个单元格对于选择代码来说都是正确的。
编辑:
从 cellForRowAtIndexPath
中删除 [result setSelected:YESanimated:YES]
并添加以下内容:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == mySelectedRow) { //mySelectedRow is the index value you stored previously.
cell.selected = YES; //or [cell setSelected:YES animated:YES] if you want it to be animated.
}
}
编辑 2:
首先调整 willDisplayCell:
中的 if 函数,以防万一:
if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {
[cell setSelected:YES animated:YES]; //or animated:NO depends on what you need.
[self performSelector:@selector(unselectCellAtIndexPath:) withObject:indexPath afterDelay:1.0]; //add a delay to the deselection. you can change the delay time to whatever suits you.
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
[cell setSelected:NO];
cell.accessoryType = UITableViewCellAccessoryNone;
}
-(void)unselectCellAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
然后在您的didSelectRowAtIndexPath:
中,选择单元格后,调用[tableView reloadData]
关于ios - 在 UITableView 的单元格中选择特定的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12058868/