UITableView設置Cell左滑多個按鈕(編輯,刪除,置頂等)


一、iOS7不支持cell多個按鈕這個時候可以使用一個三方庫JZTableViewRowAction,引用類擴展文件並實現其代理方法

 

JZTableViewRowAction下載地址:http://download.csdn.net/download/chunjunlu/9506344

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    __weak typeof(self) weakself = self;

    void(^deleteBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakself deleteAction:self.babyList[indexPath.row]];

        [weakself.babyListTable setEditing:false animated:true];

    };

  UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:deleteBabyAction];

    deleteAction.backgroundColor = [UIColor colorWithHexString:@"ff4545"];

    void(^setDefaultBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakself setDefaultBaby:self.babyList[indexPath.row]];

        [self.babyListTable setEditing:false animated:true];

    };    

    UITableViewRowAction *setDefaultAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"設為當前" handler:setDefaultBabyAction];

    setDefaultAction.backgroundColor = [UIColor colorWithHexString:@"ffa902"];

    return @[deleteAction,setDefaultAction];

}

二、iOS8以后可以直接使用系統設置

* tableView:editActionsForRowAtIndexPath: // 設置滑動刪除時顯示多個按鈕

* UITableViewRowAction // 通過此類創建按鈕

* 1. 我們在使用一些應用的時候,在滑動一些聯系人的某一行的時候,會出現刪除、置頂、更多等等的按鈕,在iOS8之前,我們都需要自己去實現。But,到了iOS8,系統已經寫好了,只需要一個代理方法和一個類就搞定了

* 2. iOS8的協議多了一個方法,返回值是數組的tableView:editActionsForRowAtIndexPath:方法,我們可以在方法內部寫好幾個按鈕,然后放到數組中返回,那些按鈕的類就是UITableViewRowAction

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

* 4. 在代理方法中,我們可以創建多個按鈕放到數組中返回,最先放入數組的按鈕顯示在最右側,最后放入的顯示在最左側

* 5. 注意:如果我們自己設定了一個或多個按鈕,系統自帶的刪除按鈕就消失了...

/設置滑動時顯示多個按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加一個刪除按鈕
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@刪除 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    NSLog(@點擊了刪除);
    //1.更新數據
    [self.dataArray removeObjectAtIndex:indexPath.row];
    //2.更新UI
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)]; 
}];
    //刪除按鈕顏色
    deleteAction.backgroundColor = [UIColor cyanColor];
    //添加一個置頂按鈕
    UITableViewRowAction *topRowAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@置頂 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@點擊了置頂);
        //1.更新數據
        [self.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        //2.更新UI
        NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
            }];    
   //置頂按鈕顏色
    topRowAction.backgroundColor = [UIColor magentaColor];
    //--------更多
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@更多 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        DetailViewController *detailVC = [[DetailViewController alloc]init];
        [self.navigationController pushViewController:detailVC animated:YES];
       
    }];
    //背景特效
    //moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
    //----------收藏
    UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@收藏handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {    
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@收藏 message:@收藏成功 delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil];
        
        [alertView show];
        [alertView release];
    }];
    //收藏按鈕顏色
     collectRowAction.backgroundColor = [UIColor greenColor];
    
    //將設置好的按鈕方到數組中返回
    return @[deleteAction,topRowAction,moreRowAction,collectRowAction];
   // return @[deleteAction,topRowAction,collectRowAction];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM