国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發之tableView實現左滑刪除功能

2019-10-21 18:49:03
字體:
來源:轉載
供稿:網友

前言

這幾天要實現左劃刪除的功能,發現網上很多帖子大多出自一人之手,然后都是 copy 的文章,其實都沒有那么復雜,只實現一個代理方法就可以了

方法如下

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { // 刪除數據源的數據,self.cellData是你自己的數據 [self.cellData removeObjectAtIndex:indexPath.row]; // 刪除列表中數據 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }}

默認刪除的文字為 Delete,要改為中文實現

- (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)

你把代理方法中這兩個方法順序搞混了,先刪除數據,再刪除 cell

[self.cellData removeObjectAtIndex:indexPath.row];這個方法在前

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];這個方法在后

還有就是,別2到沒設置代理,tableView.delegate = self;

總結

以上就是關于iOS利用tableView實現左劃刪除功能的全部內容了,希望本文的內容對給iOS開發者們能有一定的幫助,如果有疑問大家可以留言交流。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹阳市| 醴陵市| 凌云县| 西和县| 前郭尔| 清新县| 肇源县| 江西省| 林口县| 楚雄市| 稷山县| 合江县| 北川| 响水县| 尤溪县| 长寿区| 都江堰市| 乃东县| 郁南县| 苍梧县| 浦县| 临西县| 阳东县| 宜春市| 永清县| 赤水市| 武定县| 溧阳市| 景洪市| 阿巴嘎旗| 平乐县| 威远县| 繁峙县| 柘城县| 秭归县| 微山县| 侯马市| 乐安县| 滕州市| 城固县| 弥勒县|