cell的編輯模式和側(cè)滑按鈕狀態(tài)總結(jié)

  • cell的編輯模式

self.tableView.allowsMultipleSelection = YES; //允許多選
self.tableView.allowsSelection = YES; //允許選擇某cell
self.tableView.allowsSelectionDuringEditing = YES; //編輯模式下允許可以
self.tableView.allowsMultipleSelectionDuringEditing = YES; //編輯模式下是否可以多選,選中后前面出現(xiàn)對(duì)勾
(如下圖)
self.tableView.indexPathForSelectedRow; // 被選中的行

cell多選.png
  • cell右滑插入左滑刪除

    //自定義cell的編輯模式
    - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        if(){
           return UITableViewCellEditingStyleDelete; //在某行添加刪除功能
        }else{
           return UITableViewCellEditingStyleInsert; //在某行添加插入功能
        }
     }
    
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES; //設(shè)置為可編輯模式 tableview默認(rèn) editing為NO
    }
    
    //進(jìn)入編輯模式眨业,確定編輯提交操作時(shí)絮姆,執(zhí)行的代理方法
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        //刪除行
        if (editingStyle == UITableViewCellEditingStyleDelete) {
           //一定先該行刪除數(shù)據(jù)
           NoticeItem *noticeItem = [_dataSourceArr objectAtIndex:indexPath.row];
          [_http deleteNotificationAction:noticeItem.noticeId];
          [_dataSourceArr removeObject:noticeItem];
          //再去刪除此行
          dispatch_async(dispatch_get_main_queue(), ^{
              [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
          });
       }
       //插入行
       if(editingStyle == UITableViewCellEditingStyleInsert) {
            //可選擇插入的數(shù)據(jù)
            [_dataSourceArr insertObject:noticeItem atIndex:[_dataSourceArr objectAtIndex:indexPath.row]];
            //插入行
            [_tableView beginUpdates];
            [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
            num++;
            [_tableView endUpdates];
        }
    }
    
  • 移動(dòng)cell位置

    //cell右邊出現(xiàn)編輯按鈕,長(zhǎng)按可拖動(dòng)
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        //實(shí)現(xiàn)數(shù)據(jù)源的排序更換
        [_dataSourceArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    }
    
  • 如果想要左滑出現(xiàn)幾個(gè)自定義按鈕 (參考http://blog.csdn.net/lengshengren/article/details/44243473 ):

    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        //設(shè)置刪除按鈕
        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
             [self.dataSource removeObjectAtIndex:indexPath.row];
             [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    
        //設(shè)置收藏按鈕
        UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
             collectRowAction.backgroundColor = [UIColor greenColor];
             NSLog(@"收藏成功");
        }];
    
        //設(shè)置置頂按鈕
        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
             //移動(dòng)數(shù)據(jù)位次
             [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
             //改變行的位置
             NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
             [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
        }];
    
        collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        topRowAction.backgroundColor = [UIColor blueColor];
        collectRowAction.backgroundColor = [UIColor grayColor];
    
        return @[deleteRowAction,collectRowAction,topRowAction];
    }
    
  • 想要滑動(dòng)出現(xiàn)的按鈕含文字和圖片,可以使用 MGSwipeTableCell
    參考: http://www.reibang.com/p/25908a61f849

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末餐禁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子硫麻,更是在濱河造成了極大的恐慌弟断,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件龄恋,死亡現(xiàn)場(chǎng)離奇詭異疙驾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)郭毕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門它碎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事扳肛∩倒遥” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵挖息,是天一觀的道長(zhǎng)金拒。 經(jīng)常有香客問我,道長(zhǎng)套腹,這世上最難降的妖魔是什么绪抛? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮电禀,結(jié)果婚禮上幢码,老公的妹妹穿的比我還像新娘。我一直安慰自己尖飞,他們只是感情好症副,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著政基,像睡著了一般贞铣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上腋么,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天咕娄,我揣著相機(jī)與錄音,去河邊找鬼珊擂。 笑死圣勒,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的摧扇。 我是一名探鬼主播圣贸,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼扛稽,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼吁峻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起在张,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤用含,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后帮匾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體啄骇,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年瘟斜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缸夹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痪寻。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖虽惭,靈堂內(nèi)的尸體忽然破棺而出橡类,到底是詐尸還是另有隱情,我是刑警寧澤芽唇,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布顾画,位于F島的核電站,受9級(jí)特大地震影響披摄,放射性物質(zhì)發(fā)生泄漏亲雪。R本人自食惡果不足惜勇凭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一疚膊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧虾标,春花似錦寓盗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蘸吓,卻和暖如春善炫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背库继。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工箩艺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宪萄。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓艺谆,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親拜英。 傳聞我的和親對(duì)象是個(gè)殘疾皇子静汤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

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