当单元格不再被选中或其他单元格处于选中状态时,如何消除在 UITableView 中选择单元格时创建的 View 。
假设我有下面的 didSelectRowAtIndexPath 代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *selectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width, (int)(cellSize.height + 100))];
selectionView.layer.borderWidth = 2;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:selectionView];
}
现在,我想在我关注另一个单元格时删除创建的 selectionView 并再次创建它到我关注的单元格。问题是当我第一次选择一个单元格时,它工作得很好但是当我选择另一个单元格,从前一个单元格创建的 selectionView 仍然没有消失,它已经复制了 View 。我该如何解决这个问题?需要建议.. :( 谢谢..
最佳答案
您需要为selectionView
添加标签
,如下所示
selectionView.tag = 100;
此外,您需要通过声明一个 NSIndexPath
类成员并保留它来引用最后选择的 indexPath。
因此,在选择新单元格时,获取具有最后选择的索引路径的单元格,并从单元格中删除 View ,如下所示
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelIndexPath];
UIView *view = [lastCell viewWithTag:100];
[view removeFromSuperview];
关于iphone - iOS:UITableView 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073786/