iOS開發(fā)之tableView實現(xiàn)左滑刪除功能
前言
這幾天要實現(xiàn)左劃刪除的功能,發(fā)現(xiàn)網(wǎng)上很多帖子大多出自一人之手,然后都是 copy 的文章,其實都沒有那么復雜,只實現(xiàn)一個代理方法就可以了
方法如下
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 刪除數(shù)據(jù)源的數(shù)據(jù),self.cellData是你自己的數(shù)據(jù)
[self.cellData removeObjectAtIndex:indexPath.row];
// 刪除列表中數(shù)據(jù)
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
默認刪除的文字為 Delete,要改為中文實現(xiàn)
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";//默認文字為 Delete
}
下面這兩個代理方法不用寫也可以,默認就是這樣
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
如果你報了這個錯誤:
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)
你把代理方法中這兩個方法順序搞混了,先刪除數(shù)據(jù),再刪除 cell
[self.cellData removeObjectAtIndex:indexPath.row];這個方法在前
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];這個方法在后
還有就是,別2到?jīng)]設(shè)置代理,tableView.delegate = self;
總結(jié)
以上就是關(guān)于iOS利用tableView實現(xiàn)左劃刪除功能的全部內(nèi)容了,希望本文的內(nèi)容對給iOS開發(fā)者們能有一定的幫助,如果有疑問大家可以留言交流。
- iOS應(yīng)用開發(fā)中對UIImage進行截取和縮放的方法詳解
- iOS應(yīng)用開發(fā)中使用UIScrollView控件來實現(xiàn)圖片縮放
- iOS UITableView展開縮放動畫實例代碼
- iOS開發(fā)中Quartz2D控制圓形縮放和實現(xiàn)刷幀效果
- iOS實現(xiàn)點擊微信頭像(放大、縮放、保存)效果
- iOS tableView實現(xiàn)頭部拉伸并改變導航條漸變色
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 解決iOS11刷新tableview會出現(xiàn)漂移的現(xiàn)象
- IOS實現(xiàn)左右兩個TableView聯(lián)動效果
- iOS TableView頭視圖根據(jù)偏移量下拉縮放效果
相關(guān)文章
iOS面試中如何優(yōu)雅回答B(yǎng)lock導致循環(huán)引用的問題
這篇文章主要給大家介紹了iOS面試中關(guān)于如何優(yōu)雅回答B(yǎng)lock導致循環(huán)引用的問題的相關(guān)資料,文中通過圖文介紹的非常相信,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
iOS中創(chuàng)建表格類視圖WBDataGridView的實例代碼
這篇文章主要介紹了iOS中創(chuàng)建表格類視圖WBDataGridView的實例代碼,需要的朋友可以參考下2017-02-02
iOS優(yōu)化UITableViewCell高度計算的一些事兒
這iOS開發(fā)中對于UITableViewCell高度自適應(yīng)的文章已經(jīng)很多很多,但都不是自己所需要的,下面篇文章主要給大家介紹了關(guān)于iOS優(yōu)化UITableViewCell高度計算的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-11-11
iOS中UIAlertController設(shè)置自定義標題與內(nèi)容的方法
UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會有警告)。下面這篇文章主要給大家介紹了關(guān)于iOS中UIAlertController如何設(shè)置自定義標題與內(nèi)容的相關(guān)資料,需要的朋友可以參考下。2017-10-10
xcode8提交ipa失敗無法構(gòu)建版本問題的解決方案
xcode升級到xcode8后發(fā)現(xiàn)構(gòu)建不了新的版本。怎么解決呢?下面小編給大家?guī)砹藊code8提交ipa失敗無法構(gòu)建版本問題的解決方案,非常不錯,一起看看吧2016-10-10

