在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,
但是希望用户在完成交互之后,这些单元格的选中状态能够消失。
Cocoa Touch 提供了两种方法来防止单元格背持久选中。
1.cell.selectionStyle = UITableViewCellSelectionStyleNone;
该方法缺点是虽然cell可以被用户选中后,但不会被突出显示。
2.第二种方法允许单元格高亮显示,但是交互完成之后移除高亮显示。
这需要通知表格取消单元格选中状态。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消选择状态
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
代码如下:
-(void)unselectCell:(id)sender{
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//跳转等其他操作
[self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.5];
}
本文介绍如何在iOS应用中使用两种方法取消UITableView单元格的选中状态,包括设置cell.selectionStyle为UITableViewCellSelectionStyleNone及通过代码实现交互后取消单元格高亮。
1014

被折叠的 条评论
为什么被折叠?



