UITableViewCell右滑時自定義添加多個按鈕

這是iOS8新增加的一個屬性,用起來簡直方便~

typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = 0,
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
UITableViewRowActionStyleNormal
} NS_ENUM_AVAILABLE_IOS(8_0);


效果圖大概就是這樣:

![自定義置頂和收藏按鈕](http://upload-images.jianshu.io/upload_images/444873-dc68e676bf98b900.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

具體使用起來也非常方便:

//設置可以編輯

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    }
    //設置編輯操作:刪除

  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
    }
    //自定義按鈕
    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    NSLog(@"點擊了刪除");
    [tableView setEditing:NO animated:YES];
    }];
    layTopRowAction1.backgroundColor = [UIColor redColor];

    UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    NSLog(@"點擊了置頂");
    [tableView setEditing:NO animated:YES];
    }];
    layTopRowAction2.backgroundColor = [UIColor greenColor];

    UITableViewRowAction *layTopRowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    NSLog(@"點擊了更多");
    [tableView setEditing:NO animated:YES];

    }];
    layTopRowAction3.backgroundColor = [UIColor blueColor];

    NSArray *arr = @[layTopRowAction1,layTopRowAction2,layTopRowAction3];
    return arr;
    }


以上.OVER
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容