UITableView 編輯總結(jié)

代碼下載地址一
代碼下載地址二

一、總述

在iOS開發(fā)中,UITableView的使用率可以說是非常高的,所以它在iOS開發(fā)中的地位是無法替代的。最近項(xiàng)目涉及到UITableView的編輯問題,這個(gè)問題其實(shí)非常廣泛,我希望從我的角度盡可能地把這個(gè)問題思慮全面,講述清楚。

UITableView的編輯,我主要從這些方面來講述:cell的插入,刪除,選擇,移動(dòng)排序以及UITableView的編輯模式等。

二、UITableView cell的插入

UITableView cell的插入首先要保證UITableView的數(shù)據(jù)源插入了數(shù)據(jù),否則會(huì)發(fā)生奔潰。正確地插入了數(shù)據(jù)之后,可以通過一下幾種方式達(dá)到插入Cell的效果:

1.刷新UITableView,既調(diào)用UITableView的-reloadData:方法或者-reloadSections: withRowAnimation:方法,其實(shí)還有其他的一些刷新UITableView的方式。但是我要說的是使用這種方式的話性能方面不是很好,因?yàn)樗⑿耈ITableView會(huì)調(diào)用很多次代理方法。

2.直接插入Cell,既調(diào)用UITableView的insertRowsAtIndexPaths:withRowAnimation:方法或者insertSections: withRowAnimation:方法來插入。這種方式相對(duì)性能較高。

需要注意的問題:插入了Cell最好還是調(diào)用一下UITableView的-scrollToRowAtIndexPath:atScrollPosition:UITableViewScrollPositionNoneanimated:這個(gè)方法,把UITableView滾動(dòng)到對(duì)應(yīng)的位置。

代碼展示:

NSInteger row = self.dataArr.count - 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
 //UITableView 滾動(dòng)到添加的行
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];

二、UITableView cell的刪除

UITableView 的cell刪除的效果如下:
![Uploading Simulator Screen Shot 2016年10月14日 下午5.55.04_184090.png . . .]](http://upload-images.jianshu.io/upload_images/3238517-72ec984109d01951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Simulator Screen Shot 2016年10月14日 下午5.55.04.png

Simulator Screen Shot 2016年10月14日 下午5.55.05.png

UITableView 的cell刪除的步驟如下:
1.在UITableView的- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中返回YES,來確定UITableView能夠被編輯。
2.在UITableView的- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中根據(jù)編輯的類型移除掉相應(yīng)的數(shù)據(jù),然后調(diào)用- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation或者- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation這個(gè)方法來刪除Cell。
提示:可以在- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中設(shè)置刪除按鈕的文字,否則默認(rèn)為delete。

代碼展示:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        
    }
    else
    {
        
    }
}

UITableView的編輯模式

設(shè)置UITableView的editing屬性為YES可以進(jìn)入編輯模式,UITableView的編輯模式有幾種狀態(tài),在UITableView的- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中設(shè)置,可以返回一下幾種狀態(tài):

  • UITableViewCellEditingStyleNone:cell往右縮進(jìn),但是左邊不出現(xiàn)任何控件
  • UITableViewCellEditingStyleDelete:cell往右縮進(jìn),但是左邊出現(xiàn)紅色減號(hào)控件
  • UITableViewCellEditingStyleInsert:cell往右縮進(jìn),但是左邊出現(xiàn)藍(lán)色加號(hào)控件
  • UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert:cell往右縮進(jìn),但是左邊出現(xiàn)選擇控件


    Simulator Screen Shot 2016年10月16日 下午3.37.04.png

    在UITableView的- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中對(duì)編輯模式時(shí)的操作(處理選擇操作)進(jìn)行處理,代碼如下:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row%4) {
        case 0:
            return UITableViewCellEditingStyleNone;
            break;
        case 1:
            return UITableViewCellEditingStyleDelete;
            break;
        case 2:
            return UITableViewCellEditingStyleInsert;
            break;
            
        default:
            return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
            break;
    }
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleNone) {
        
    }
    else if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加計(jì)劃" message:@"請(qǐng)輸入明日計(jì)劃" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        
    }
}

UITableView cell的選擇

Simulator Screen Shot 2016年10月16日 下午5.52.58.png
Simulator Screen Shot 2016年10月16日 下午5.53.00.png

UITableView cell的選擇,我們只需要如下幾步操作即可:
1.進(jìn)入編輯模式(前面已經(jīng)講過)
self.tableView.editing = YES;
2.在UITableView的- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)代理方法中設(shè)置編輯模式為選擇的類型

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     UITableView 編輯狀態(tài)的樣式有如下三種:
        UITableViewCellEditingStyleNone:cell往右縮進(jìn),但是左邊不出現(xiàn)任何控件
        UITableViewCellEditingStyleDelete:cell往右縮進(jìn),但是左邊出現(xiàn)紅色減號(hào)控件
        UITableViewCellEditingStyleInsert:cell往右縮進(jìn),但是左邊出現(xiàn)藍(lán)色加號(hào)控件
        UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert:cell往右縮進(jìn),但是左邊出現(xiàn)選擇控件
     */
        return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
  }

3.在UITableView的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath和

  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath這兩個(gè)代理方法中對(duì)數(shù)據(jù)進(jìn)行操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!tableView.editing) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else
    {
        [self.selectedDataArr addObject:self.dataArr[indexPath.row]];
    }
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.editing) {
        [self.selectedDataArr removeObject:self.dataArr[indexPath.row]];
    }
}

4.完成選擇后的操作,這主要根據(jù)需求而定,我在這里做了一個(gè)刪除操作

            //插入數(shù)據(jù),刷新界面
            NSMutableArray *deleteArr = [NSMutableArray arrayWithCapacity:1];
            NSIndexPath *deleteIndexPath = nil;
            for (NSString *str in self.selectedDataArr) {
                deleteIndexPath = [NSIndexPath indexPathForRow:[self.dataArr indexOfObject:str] inSection:0];
                [deleteArr addObject:deleteIndexPath];
            }
            [self.dataArr removeObjectsInArray:self.selectedDataArr];
            [self.selectedDataArr removeAllObjects];
            [self.tableView deleteRowsAtIndexPaths:deleteArr withRowAnimation:UITableViewRowAnimationNone];

UITableView cell的移動(dòng)排序

Simulator Screen Shot 2016年10月16日 下午5.55.06.png
Simulator Screen Shot 2016年10月16日 下午5.55.22.png

UITableView cell的排序操作其實(shí)和選擇操作非常類似,首先也是進(jìn)入編輯模式,然后在UITableView的- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath的這個(gè)代理方法中返回YES,讓UITableView可以移動(dòng),最后在UITableView的- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath這個(gè)代理方法中對(duì)排序之后的數(shù)據(jù)進(jìn)行操作即可

//設(shè)置tableView編輯狀態(tài)
self.tableView.editing = YES;

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *exchangeStr = self.dataArr[sourceIndexPath.row];
    [self.dataArr removeObjectAtIndex:sourceIndexPath.row];
    [self.dataArr insertObject:exchangeStr atIndex:destinationIndexPath.row];
}

UITableView 右滑cell出現(xiàn)多個(gè)按鈕

Simulator Screen Shot 2016年10月16日 下午11.46.10.png

UITableView 右滑cell出現(xiàn)多個(gè)按鈕在iOS8之前是不支持的,需要自己去實(shí)現(xiàn),在iOS8以后,UITableView的代理中多了這樣一個(gè)方法- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath和這樣一個(gè)類UITableViewRowAction,一個(gè)UITableViewRowAction對(duì)象表示一個(gè)按鈕,按鈕的點(diǎn)擊操作包裝在UITableViewRowAction創(chuàng)建方法的block中,在代理方法中返回UITableViewRowAction的數(shù)組就可以了,最先放入數(shù)組的按鈕顯示在最右側(cè),最后放入的顯示在最左側(cè)。

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //添加一個(gè)刪除按鈕
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //處理數(shù)據(jù)
        [self.dataArr removeObjectAtIndex:indexPath.row];
        //更新UI
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    
    //添加一個(gè)上移按鈕
    UITableViewRowAction *upAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"上移" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        if (indexPath.row > 0) {
            //處理數(shù)據(jù)
            [self.dataArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:indexPath.row - 1];
            //更新UI
            [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
    
    //添加一個(gè)下移按鈕
    UITableViewRowAction *downAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"下移" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        if (indexPath.row < self.dataArr.count - 1) {
            [self.dataArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:indexPath.row + 1];
            //更新UI
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
    
    //設(shè)置背景顏色
    downAction.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1];
    
    //放回?cái)?shù)組返回
    return @[deleteAction, upAction, downAction];
}

需要注意的是:如果我們自己使用UITableViewRowAction設(shè)定了一個(gè)或多個(gè)按鈕,系統(tǒng)自帶的刪除按鈕就失效了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,748評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,165評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,595評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,633評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,435評(píng)論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,943評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,035評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,175評(píng)論 0 287
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,713評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,599評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,788評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,303評(píng)論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,034評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,412評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,664評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,408評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,747評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容