溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS中給UITableView的側(cè)滑刪除增加多個按鈕的實現(xiàn)方法

發(fā)布時間:2020-09-08 01:53:39 來源:腳本之家 閱讀:661 作者:oiken 欄目:移動開發(fā)

一. 需求:

cell的側(cè)滑刪除默認(rèn)只有一個刪除按鈕, 給側(cè)滑添加多個按鈕, '刪除', '置頂', '更多'.

 二. 實現(xiàn)說明:

1) 我們在使用一些應(yīng)用的時候,在滑動一些聯(lián)系人的某一行的時候,會出現(xiàn)刪除、置頂、更多等等的按鈕,在iOS8之前,我們都需要自己去實現(xiàn)。但是到了iOS8,系統(tǒng)已經(jīng)寫好了,只需要一個代理方法和一個類就搞定了

2) iOS8的協(xié)議多了一個方法,返回值是數(shù)組的tableView:editActionsForRowAtIndexPath:方法,我們可以在方法內(nèi)部寫好幾個按鈕,然后放到數(shù)組中返回,那些按鈕的類就是UITableViewRowAction

3) 在UITableViewRowAction類,我們可以設(shè)置按鈕的樣式、顯示的文字、背景色、和按鈕的事件(事件在Block中實現(xiàn))

4) 在代理方法中,我們可以創(chuàng)建多個按鈕放到數(shù)組中返回,最先放入數(shù)組的按鈕顯示在最右側(cè),最后放入的顯示在最左側(cè)

5) 注意:如果我們自己設(shè)定了一個或多個按鈕,系統(tǒng)自帶的刪除按鈕就消失了.

三. 代碼實現(xiàn):

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // 添加一個'刪除'按鈕
 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  NSLog(@"點擊了'刪除'");
  // 1. 更新數(shù)據(jù)
  // 2. 更新UI
//  [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
 // 添加一個'置頂'按鈕
 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  NSLog(@"點擊了'置頂'");
  // 1. 更新數(shù)據(jù)
  // 2. 更新UI
  //NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
  //[tableView moveRowAtIndexPath:indexPathtoIndexPath:firstIndexPath];
  }];
 topRowAction.backgroundColor = [UIColor blueColor];
 // 添加一個'更多'按鈕
 UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  NSLog(@"點擊了'更多'");
  [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
 }];
 moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
 //將設(shè)置好的按鈕放到數(shù)組中返回
 return @[deleteRowAction, topRowAction, moreRowAction];
}

相關(guān)閱讀:

詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法

IOS中UITableView滾動到指定位置

以上所述是小編給大家介紹的Android中給UITableView的側(cè)滑刪除增加多個按鈕的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI