IOS UITableViewCell "Swipe to appear multiple buttons"

Source: Internet
Author: User

This article is authorized reprint, @ Summer is an adult

Preface: This blog is actually want to introduce Tableviewcell sliding some "things", yesterday in the trip to GitHub saw a very interesting three-party library, simple with a feel good, a record, the second is hoping to have similar needs can be helped.

This article describes how iOS 5 (using a three-party library) after iOS 8 (System method) is implemented separately

-ios>= 5.0

-Ios>= 8.0

Mgswipetablecell (three-party library on GitHub)-IOS >= 5.0

Direct use comparison simple to look through the code

First sign this Agreement mgswipetablecelldelegate

Add Left button method

12345678910111213141516 - (NSArray *)btnLeftCount:(int)count{    NSMutableArray *result = [NSMutableArray array];    UIColor *colors[3] = {[UIColor greenColor],        [UIColor colorWithRed:0 green:0x99/255.0 blue:0xcc/255.0 alpha:1.0],        [UIColor colorWithRed:0.59 green:0.29 blue:0.08 alpha:1.0]};;    for(int i = 0; i < count; i ++) {        // 按钮提供了几个方法, 可以点进去看一看        MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:@""backgroundColor:colors[i] padding:15 callback:^BOOL(MGSwipeTableCell *sender) {            returnYES;        }];        // 把按钮加到数组中        [result addObject:btn];    }    returnresult;}

Ways to add the right button

123456789101112131415 - (NSArray *)btnRightCount:(int)count{    NSMutableArray *result = [NSMutableArray array];    NSArray *titleArray = @[@"删除", @"标记未读"];    UIColor *color[2] = {[UIColor redColor], [UIColor lightGrayColor]};    for(int i = 0; i < count; i ++) {        MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:titleArray[i] backgroundColor:color[i] padding:15 callback:^BOOL(MGSwipeTableCell *sender) {            BOOL autoHide = i != 0;            returnautoHide;        }];        // 把按钮加到数组中        [result addObject:btn];    }    returnresult;}

Reuse pools can be written like this

1234567891011121314151617 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellID = @"cellId";    // 这里如果MGSwipeTableCell是足够你使用的, 你可以直接使用    // 或者自定义创建cell继承于MGSwipeTableCell, 像我下面代码这样    XTCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];    if(!cell) {        cell = [[XTCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];    }    cell.label.text = [NSString stringWithFormat:@"%ld------%@", (long)indexPath.row, self.arrayTest[indexPath.row]];    cell.label.font = [UIFont systemFontOfSize:20];    // 指定代理人    cell.delegate = self;    // NO: 只有单个可以滑动 , YES: 多个可以滑动    cell.allowsMultipleSwipe = NO;    returncell;}

Add button

12345678910 -(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction             swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;{    if(direction == MGSwipeDirectionRightToLeft) {        return[self btnRightCount:2];    }    else{        return[self btnLeftCount:3];    }}

Button's Click Proxy method

123456789101112131415161718192021222324252627 -(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion{    switch(direction) {        caseMGSwipeDirectionLeftToRight: {            if(index == 0) {                NSLog(@"right ------- 0");            }else{                NSLog(@"right ------- 1");            }            break;        }        caseMGSwipeDirectionRightToLeft: {            if(index == 0) {                NSLog(@"left ------- 0");                // 这里简单的做了个删除操作                NSIndexPath * path = [_tableView indexPathForCell:cell];                [_arrayTest removeObjectAtIndex:path.row];                [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];                return NO;            }else{                NSLog(@"left ------- 1");            }            break;        }    }    returnYES;}

Similar implementations are available after IOS 8

1234567891011121314151617 - (NSArray<uitableviewrowaction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {        [self.arrayTest removeObjectAtIndex:indexPath.row];        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    }];    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {        [self.arrayTest exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];    }];    topRowAction.backgroundColor = [UIColor blueColor];    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];    }];    return@[deleteRowAction,topRowAction,moreRowAction];

IOS UITableViewCell "Swipe to appear multiple buttons"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.