IOS表格视图选择行不工作
我正在尝试向选定行添加复选标记,并在再次选中该行时将其删除,但每次我选择一行时,复选标记只会转到第一个单元格,然后向下移动(即我单击任何单元格,然后单元格0有一个复选标记,我单击任何其他单元格,单元格1有一个复选标记,等等) 以下是我的DIDSelectRowatineXpath和CellForRowatineXpath方法: 更新:IOS表格视图选择行不工作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试向选定行添加复选标记,并在再次选中该行时将其删除,但每次我选择一行时,复选标记只会转到第一个单元格,然后向下移动(即我单击任何单元格,然后单元格0有一个复选标记,我单击任何其他单元格,单元格1有一个复选标记,等等) 以下是我的DIDSelectRowatineXpath和CellForRowatineXpath方法: 更新: // the cell will be returned to the tableView - (UITableViewCell *)tableView:(UIT
// 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];
}
您有[tableView reloadData]这将清除tableView的当前状态 重新加载表视图将清除当前状态,包括当前选择。但是,如果显式调用reloadData,它将清除此状态,并且对LayoutSubView的任何后续直接或间接调用都不会触发重新加载 更多信息 更新 您正在didSelectRowAtIndexPath和cellForRowAtIndexPath中创建tableviewcells。你为什么这么做?这将写入选定的单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
您有[tableView reloadData]这将清除tableView的当前状态 重新加载表视图将清除当前状态,包括当前选择。但是,如果显式调用reloadData,它将清除此状态,并且对LayoutSubView的任何后续直接或间接调用都不会触发重新加载 更多信息 更新 您正在didSelectRowAtIndexPath和cellForRowAtIndexPath中创建tableviewcells。你为什么这么做?这将写入选定的单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
您不应该通过select方法中的deque方法获取单元格,因为您获取的单元格不是您接触到的单元格,请像取消选择方法一样使用cellForCellAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
您不应该通过select方法中的deque方法获取单元格,因为您获取的单元格不是您接触到的单元格,请像取消选择方法一样使用cellForCellAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
可能是因为重新加载数据?重新加载表视图将清除当前状态,包括当前选择。但是,如果显式调用reloadData,它将清除此状态,并且对LayoutSubView的任何后续直接或间接调用都不会触发重新加载。不要将代码作为图像发布。用实际代码更新您的问题(请正确格式化)。从
didselectRowatinedexpath
中删除[tableView reloadData]
。我尝试删除reload data方法,现在所发生的只是行高亮显示一秒钟,然后取消高亮显示本身。我还尝试删除了DecelrowatindextPath调用,但现在出现的情况是,该框被选中,并且变灰,文本不再可见。当我这样做时,我无法取消选择tablebox(这也应该是可能的)@rmaddy我想你的意思是“你不应该将代码作为图像发布,这会给版主带来问题。我们可以选择添加代码,你应该改为使用。”这样一来,你的攻击性就降低了,你会让我想把它改成代码,而不是想再上传10张代码图片来骚扰你。可能是因为重新加载了数据?重新加载表视图将清除当前状态,包括当前选择。但是,如果显式调用reloadData,它将清除此状态,并且对LayoutSubView的任何后续直接或间接调用都不会触发重新加载。不要将代码作为图像发布。用实际代码更新您的问题(请正确格式化)。从didselectRowatinedexpath
中删除[tableView reloadData]
。我尝试删除reload data方法,现在所发生的只是行高亮显示一秒钟,然后取消高亮显示本身。我还尝试删除了DecelrowatindextPath调用,但现在出现的情况是,该框被选中,并且变灰,文本不再可见。当我这样做时,我无法取消选择tablebox(这也应该是可能的)@rmaddy我想你的意思是“你不应该将代码作为图像发布,这会给版主带来问题。我们可以选择添加代码,你应该改为使用。”这样一来,你的攻击性就降低了,你会让我想把它改成代码,而不是想再上传10张代码图片来打扰你。删除它仍然不会给选中的行添加复选标记。该行仅高亮显示一秒钟,然后取消高亮显示,到此结束,没有选中标记没有问题!很高兴您修复了它。删除它仍然不会向所选行添加复选标记。该行仅高亮显示一秒钟,然后取消高亮显示,到此结束,没有选中标记没有问题!很高兴你修好了。