作者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)
推 charlesdc:table reladdata也行 01/16 21:54
推 sorkayi:改變 data source 後 在 reloaddata 01/17 10:09
推 whitefur:謝u大 01/18 00:17
→ whitefur:一二樓的方法效能比較差吧@@? 01/18 00:18
→ uranusjr:其實實務上通常沒差那麼一點, 我個人覺得最大的差異是有 01/18 11:53
→ uranusjr:animation 看起來比較帥XD (蘋果也建議如果更新是由使用 01/18 11:54
→ uranusjr:者觸發的, 用一個動畫更新會比 reloadData 好) 01/18 11:54