作者uranusjr (←这人是超级笨蛋)
看板MacDev
标题Re: [问题] 自订删除tableviewCell的button
时间Wed Jan 16 08:26:58 2013
※ 引述《whitefur (白毛)》之铭言:
: 如何按下在UITableViewCell的UIButton後delete该cell
: ex:以下是一个UITableView
: _____________
: | x|
: |___________|
: | x|
: |___________|
: | x|
: |___________|
: 每个cell右上角有一个UIButton
: 我想要按下该button後将该cell删除
: 我的解法:
: 在按下button後
: 执行[self setEditing:YES animated:YES]
: (这里的self是指UITableViewCell instance)
: 但是都没作用....
: 请问这该如何解决呢
: 谢谢
1. 要能进入 editing mode, 你必须设定那个 cell 的 editing accessory type
2. 不过你想要的是在按下按钮後「直接删除」, 所以你不应该进入 editing mode
- (void)deleteTableViewRowForButton:(id)sender
{
// 拿到 button 所在的 cell
UITableViewCell *cell = [self tableViewCellForButton:sender];
UITableView *table = cell.superview;
NSIndexPath *cellIndex = [table indexPathForCell:cell];
[table beginUpdates];
[table deleteRowsAtIndexPath:@[cellIndex] animated:YES];
[table endUpdates];
}
直接删就好了
不过要记得, 删 cell 的时候也要同时处理你的 data source 让它们同步...
--
"问おう、贵方が私のマスターか?"
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.32.81.146
※ 编辑: uranusjr 来自: 114.32.81.146 (01/16 08:27)
1F:推 charlesdc:table reladdata也行 01/16 21:54
2F:推 sorkayi:改变 data source 後 在 reloaddata 01/17 10:09
3F:推 whitefur:谢u大 01/18 00:17
4F:→ whitefur:一二楼的方法效能比较差吧@@? 01/18 00:18
5F:→ uranusjr:其实实务上通常没差那麽一点, 我个人觉得最大的差异是有 01/18 11:53
6F:→ uranusjr:animation 看起来比较帅XD (苹果也建议如果更新是由使用 01/18 11:54
7F:→ uranusjr:者触发的, 用一个动画更新会比 reloadData 好) 01/18 11:54