cell 滑動(dòng) --> UITableViewRowAction

iOS 8之前使用如下方式自定義UITableView左劃后顯示的文字,不過(guò)該樣式太單一了,而且只能顯示一個(gè):

- (nullable NSString *)tableView:(UITableView *)tableView 
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

iOS8之后,只需要一個(gè)tableView代理方tableView:editActionsForRowAtIndexPath:和一個(gè)類(lèi)UITableViewRowAction就可以了。 rowAction可以設(shè)置style、title、backgroundColor、backgroundEffect,在block中實(shí)現(xiàn)點(diǎn)擊事件

注意 : title 可以設(shè)置,但是title的文字顏色沒(méi)辦法設(shè)置。
其中不修改backgroundColor時(shí),backgroundColor的顏色是由style決定的.

  • UITableViewRowActionStyleDestructive時(shí)是紅色的刪除樣式,
  • UITableViewRowActionStyleNormal時(shí)是灰色樣式,類(lèi)似于微信好友列表左劃后的“備注”。
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

當(dāng)實(shí)現(xiàn)該代理方法后,以下的這個(gè)代理方法就不執(zhí)行了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //設(shè)置刪除按鈕
    UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        [self.arrremoveObjectAtIndex:indexPath.row];
       [self.tableViewdeleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }];
    
    //設(shè)置收藏按鈕
    UITableViewRowAction *collectRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"收藏"message:@"收藏成功"delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
        [alertView show];
    }];
    //設(shè)置置頂按鈕
    UITableViewRowAction *topRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"置頂"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        [self.arrinsertObject:self.arr[indexPath.row]atIndex:0];
        [self.arrremoveObjectAtIndex:indexPath.row +1];
        NSIndexSet *set = [NSIndexSetindexSetWithIndex:0];
        [tableView reloadSections:setwithRowAnimation:UITableViewRowAnimationTop];
        /**
         *  tableView 刷新時(shí)如果確定哪個(gè)row 或者section就刷新對(duì)應(yīng)的,不要走reloadData
         */
    }];

    topRowAction.backgroundColor = [UIColorblueColor];
    collectRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
    return  @[deleteRowAction,collectRowAction,topRowAction];
}

參考 :https://blog.csdn.net/mlcldh/article/details/54947302
https://blog.csdn.net/textfielddelegate/article/details/50599151
https://www.cnblogs.com/zj901203/p/4606804.html
https://blog.csdn.net/zhanglizhi111/article/details/52922915

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在此特此聲明:一下所有鏈接均來(lái)自互聯(lián)網(wǎng),在此記錄下我的查閱學(xué)習(xí)歷程,感謝各位原創(chuàng)作者的無(wú)私奉獻(xiàn) ! 技術(shù)一點(diǎn)一點(diǎn)積...
    遠(yuǎn)航的移動(dòng)開(kāi)發(fā)歷程閱讀 11,544評(píng)論 12 197
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評(píng)論 25 709
  • 遠(yuǎn)房侄子上一年級(jí),昨天到家里來(lái)玩。說(shuō)到期末考試成績(jī)?!翱嫉恼樱俊薄拔覕?shù)學(xué)考的不好?!薄岸嗌俜??”他有點(diǎn)不好意思的...
    墨行川閱讀 464評(píng)論 0 0
  • 何事惹人愁,薄涼似深秋。聽(tīng)葉落,瑟瑟到心頭。殘花飛過(guò)溪邊柳,聲聲問(wèn),盼可留。 絲雨落輕舟,兩岸...
    請(qǐng)叫我想念熊閱讀 240評(píng)論 3 2
  • 前世的五百次回眸才能換得今生的一次擦肩而過(guò),那么,我要用多少次回眸才能真正住進(jìn)你的心中?(——席慕蓉) 人的...
    阿John閱讀 263評(píng)論 0 1

友情鏈接更多精彩內(nèi)容