iOS tableView右滑顯示選擇,iostableview右滑

來源:互聯網
上載者:User

iOS tableView右滑顯示選擇,iostableview右滑

 如何使用UITableViewRowAction實現右滑選擇呢?

1、在iOS8以前,我們實現tableview中滑動顯示刪除,置頂,更多等等的按鈕時,都需要自己去實現,在iOS8中系統已經寫好了,只要一個代理方法和一個類就行了

2、iOS8的協議對了一個方法,傳回值是數組的tableview:editActionForRowAtIndexPath:方法,我們可以在方法內部寫好幾個按鈕,然後放到數組中返回,那些按鈕的類就是UITableviewRowAction

3、在UITableviewRowAction類。我們可以設定按鈕的樣式,顯示文字、背景色和按鈕事件(在block內實現)

4、在代理方法中,我們可以常見多個按鈕放到數組中返回,最先放入數組的按鈕顯示在最右邊,最後放入的顯示在最左邊

5、如果自己設定一個或多個按鈕,系統內建的刪除按鈕就消失了

 

設定tableView可以編輯

- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

UITableViewRowAction的使用方法:

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

 

重寫UITableViewDelegate的

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

方法。

 

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{        if(indexPath.row==0){        // 添加一個刪除按鈕        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了刪除");                    }];            }    else if (indexPath.row==1){        // 添加一個刪除按鈕        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了刪除");                    }];                // 添加一個修改按鈕        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];            }    else if (indexPath.row==2){        // 添加一個刪除按鈕        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了刪除");                    }];                // 添加一個修改按鈕        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];                // 添加一個發送按鈕                sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"發送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了發送");        }];        sanRowAction.backgroundColor=[UIColor orangeColor];    }    else{        // 添加一個刪除按鈕        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了刪除");                    }];                // 添加一個修改按鈕        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];                // 添加一個發送按鈕               sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"發送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了發送");        }];        sanRowAction.backgroundColor=[UIColor orangeColor];        // 添加一個發送按鈕                OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK鍵" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"點擊了OK");        }];        OK.backgroundColor=[UIColor purpleColor];            }            // 將設定好的按鈕放到數組中返回    if (indexPath.row==0) {        return @[deleteRowAction];    }else if (indexPath.row==1){        return @[deleteRowAction,moreRowAction];    }else if(indexPath.row==2){        return @[deleteRowAction,moreRowAction,sanRowAction];    }else if(indexPath.row==3){        return @[deleteRowAction,moreRowAction,sanRowAction,OK];            }    return nil;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.