Ios 在UITableView的单元格中选择特定的字符串值
我有一个表视图,其中包含AppDelegate中的项目列表。表格视图允许用户选择他们想要的项目,并且在进行选择后,项目旁边有一个复选标记。接下来,用户可以返回上一个屏幕,此选择更新显示给用户的字符串值。此值也存储在AppDelegate中。一切正常,但如果用户选择返回屏幕,用户可以更改其选择,我希望自动选择此值 下面是我的SetSizeViewController.m中的CellFroRowatineXpath函数 获取appDelegate.selectedSize中存储的值时遇到问题。例如,该值可能类似于“Meter”Ios 在UITableView的单元格中选择特定的字符串值,ios,uitableview,Ios,Uitableview,我有一个表视图,其中包含AppDelegate中的项目列表。表格视图允许用户选择他们想要的项目,并且在进行选择后,项目旁边有一个复选标记。接下来,用户可以返回上一个屏幕,此选择更新显示给用户的字符串值。此值也存储在AppDelegate中。一切正常,但如果用户选择返回屏幕,用户可以更改其选择,我希望自动选择此值 下面是我的SetSizeViewController.m中的CellFroRowatineXpath函数 获取appDelegate.selectedSize中存储的值时遇到问题。例如,
- (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;
}
}
}
您应该避免调用didselectrowatinexpath:yourself。无论如何,您应该尝试调用[result setSelected:YES animated:YES]
另外,您如何检查此单元格是否是要选择的单元格?如果我错了,请纠正我,但在我看来,使用这个ifappDelegate.selectedSize{…,对于选择代码,每个单元格都是真的
编辑:
从cellForRowAtIndexPath中删除[result setSelected:YES animated: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]例如,是否有类似indexPath of StringValue的内容?我仍然会得到与以前相同的行为。该行会高亮显示并选中,但高亮显示不会消失。如果我将setSelected设置为“否”,则会在选择时选中该行,并且效果良好。理想情况下,我希望该行只高亮显示一秒钟,并且不会消失然后高光消失,复选标记保留。