我想创建一个用户可以选中的表,并使用复选标记取消选择:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
...;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...;
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
...;
}
Run Code Online (Sandbox Code Playgroud)
当我再次点击勾选标记的单元格时,我试图删除复选标记,但只需要点击2次而不是一次.
如果我将选择样式设置为默认值,当我单击所选行时,它会删除蓝色突出显示; 再次单击,它会删除复选标记.
我也尝试过一些条件语句didSelectRowAtIndexPath,但它们只响应第二次点击.
导致问题的原因是什么?如何解决?
Oli*_*ver 15
你可以尝试这个:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];
if (index != NSNotFound) {
UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
if ([cell accessoryType] == UITableViewCellAccessoryNone) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该在每次触摸时切换单元格的复选标记.如果您另外只想要一次选择一个单元格,还要添加以下内容:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];
if (index != NSNotFound) {
UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不想要蓝色hilight背景,只需在UITableViewCellSelectionStyleNone创建单元格后将单元格的选择样式设置为.
| 归档时间: |
|
| 查看次数: |
5870 次 |
| 最近记录: |