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

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)之tableView實現(xiàn)左滑刪除功能

2020-07-26 02:58:52
字體:
供稿:網(wǎng)友

前言

這幾天要實現(xiàn)左劃刪除的功能,發(fā)現(xiàn)網(wǎng)上很多帖子大多出自一人之手,然后都是 copy 的文章,其實都沒有那么復(fù)雜,只實現(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ā)者們能有一定的幫助,如果有疑問大家可以留言交流。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 婺源县| 东源县| 天气| 宁化县| 和平区| 东明县| 伊宁市| 龙陵县| 石城县| 临沧市| 温州市| 江达县| 元阳县| 奉贤区| 塘沽区| 琼结县| 福清市| 虹口区| 涞源县| 泸西县| 潞城市| 拉孜县| 故城县| 梁河县| 普兰店市| 平安县| 高邮市| 油尖旺区| 旌德县| 凯里市| 阳春市| 宿松县| 岳阳县| 许昌县| 大渡口区| 黄大仙区| 睢宁县| 洪洞县| 社会| 东乡县| 赫章县|