欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年01月13日 15:46:43   作者:IOSMan  
我們在使用一些應(yīng)用的時(shí)候,在滑動一些聯(lián)系人的某一行的時(shí)候,會出現(xiàn)刪除、置頂、更多等等的按鈕,下面這篇文章主要就介紹了iOS用tableView實(shí)現(xiàn)左劃刪除功能的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。

前言

這幾天要實(shí)現(xiàn)左劃刪除的功能,發(fā)現(xiàn)網(wǎng)上很多帖子大多出自一人之手,然后都是 copy 的文章,其實(shí)都沒有那么復(fù)雜,只實(shí)現(xiàn)一個(gè)代理方法就可以了

方法如下

- (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];
 }

}

默認(rèn)刪除的文字為 Delete,要改為中文實(shí)現(xiàn)

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
 return @"刪除";//默認(rèn)文字為 Delete
}

下面這兩個(gè)代理方法不用寫也可以,默認(rèn)就是這樣

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
 return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
 return YES;
}

如果你報(bào)了這個(gè)錯(cuò)誤:

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

你把代理方法中這兩個(gè)方法順序搞混了,先刪除數(shù)據(jù),再刪除 cell

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

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

還有就是,別2到?jīng)]設(shè)置代理,tableView.delegate = self;

總結(jié)

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

相關(guān)文章

最新評論