【发布时间】:2016-04-29 04:54:32
【问题描述】:
我正在尝试将复选标记添加到选定行并在再次选择时将其删除,但每次我选择一行时,复选标记只会转到第一个单元格并在此之后向下移动(即我单击任何单元格,然后单元格 0 有一个复选标记,我单击任何其他单元格,单元格 1 有一个复选标记,等等...)。
这是我的 didselectrowatindexpath 和 cellforrowatindexpath 方法:
更新:
// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"preflabel"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", data[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
【问题讨论】:
-
可能是因为reloadData?重新加载表格视图会清除当前状态,包括当前选择。但是,如果您显式调用 reloadData,它会清除此状态,并且任何后续对 layoutSubviews 的直接或间接调用都不会触发重新加载。
-
不要将您的代码作为图片发布。使用实际代码更新您的问题(并请正确格式化)。
-
从
didSelectRowAtIndexPath中删除[tableView reloadData]。 -
我尝试删除重新加载数据方法,现在所发生的只是行突出显示一秒钟,然后取消突出显示本身。我还尝试删除 deselectrowatindextpath 调用,但现在发生的情况是该框已选中并且也变灰,并且文本不再可见。当我这样做时,我无法取消选择表格框(这也应该是可能的)
-
@rmaddy 我认为您的意思是说“您不应该将代码作为图像发布,这会给版主带来问题。我们可以选择添加您应该使用的代码。”这样一来,您的攻击性就会降低,并且您会让我想将其更改为代码,而不是想再上传 10 张代码图像来打扰您。
标签: ios objective-c uitableview