【发布时间】:2015-10-12 05:53:11
【问题描述】:
我有一个UITableView,其中包含所有国家/地区的名称。
用户可以随时点击cell删除或编辑国家名称。
我的 UITableView 单元格最初看起来像这样:
现在当用户点击它时,我正在改变它:
我认为我正在遵循一种非常蹩脚的方法。这就是我所做的:
声明要添加的全局按钮:
UIButton *btnDeleteWithImage,*btnDeleteWithText,*btnEditWithImage,*btnEditWihtText; //buttons
还有一个NSMutableArray 来跟踪indexPath.row
现在在我的 didSelectMethod 中我正在这样做:
//To change the background
UIView *selectionBackground = [[UIView alloc] init];
selectionBackground.backgroundColor = [UIColor customColor];
cell.selectedBackgroundView = selectionBackground;
// to check which cell is pressed
if([indexPathCollection containsObject:index])
{
[btnDeleteWithImage removeFromSuperview];
[btnDeleteWithText removeFromSuperview];
[btnEditWihtText removeFromSuperview];
[btnEditWithImage removeFromSuperview];
[indexPathCollection removeObject:index];
[cell addSubview:btnDeleteWithImage];
[cell addSubview:btnDeleteWithText];
[cell addSubview:btnEditWithImage];
[cell addSubview:btnEditWihtText];
[indexPathCollection addObject:index];
}
else
{
[cell addSubview:btnDeleteWithImage];
[cell addSubview:btnDeleteWithText];
[cell addSubview:btnEditWithImage];
[cell addSubview:btnEditWihtText];
[indexPathCollection addObject:index];
}
但这不好用。当我滚动表格时,随机出现编辑和删除按钮。
是否有人有更好的想法如何以非常有效的方式实现这一目标。
【问题讨论】:
-
您的方法不起作用,因为您可能在 cellforrowatindexpath 委托方法中重用了单元格。
-
我该怎么做...@elbuild 请帮助我
标签: ios objective-c uitableview tableviewcell