Re: [問題] 自訂刪除tableviewCell的button

看板MacDev作者 (←這人是超級笨蛋)時間12年前 (2013/01/16 08:27), 編輯推噓3(304)
留言7則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《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)

01/16 21:54, , 1F
table reladdata也行
01/16 21:54, 1F

01/17 10:09, , 2F
改變 data source 後 在 reloaddata
01/17 10:09, 2F

01/18 00:17, , 3F
謝u大
01/18 00:17, 3F

01/18 00:18, , 4F
一二樓的方法效能比較差吧@@?
01/18 00:18, 4F

01/18 11:53, , 5F
其實實務上通常沒差那麼一點, 我個人覺得最大的差異是有
01/18 11:53, 5F

01/18 11:54, , 6F
animation 看起來比較帥XD (蘋果也建議如果更新是由使用
01/18 11:54, 6F

01/18 11:54, , 7F
者觸發的, 用一個動畫更新會比 reloadData 好)
01/18 11:54, 7F
文章代碼(AID): #1GzVFLOS (MacDev)
文章代碼(AID): #1GzVFLOS (MacDev)