MGSwipeTableCell的使用

MGSwipeTableCell的使用

 

  侧滑UITableViewCell展示多个可操作按钮是iOS开发中常用到的一个功能。这里有个非常强大的开源库:MGSwipeTableCell,可以实现此功能。其效果如下图所示:

strip

示意图.gif

引入工程

■CocoaPods 引入
  建议使用,没有使用过CocoaPods的童鞋可以参照大神唐巧的这篇文章用CocoaPods做iOS程序的依赖管理

■手动引入
  下载地址MGSwipeTableCell

使用

  可以直接用MGSwipeTableCellcell,也可以继承于MGSwipeTableCell,下面是一个使用的例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuseIdentifier = @"programmaticCell";
    MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell)
    {
        cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }

    cell.textLabel.text = @"Title";
    cell.detailTextLabel.text = @"Detail text";
    cell.delegate = self; //optional

    //configure left buttons
    cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"check.png"] backgroundColor:[UIColor greenColor]],
                         [MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"fav.png"] backgroundColor:[UIColor blueColor]]];

    cell.leftSwipeSettings.transition = MGSwipeTransition3D;

    //configure right buttons
    cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"Delete" backgroundColor:[UIColor redColor]],
                          [MGSwipeButton buttonWithTitle:@"More" backgroundColor:[UIColor lightGrayColor]]];
    cell.rightSwipeSettings.transition = MGSwipeTransition3D;
    return cell;
}

  cell.leftButtons 和 cell.rightButtons都是一个数组,可以动态填写按。 

  cell.rightSwipeSettings.transition是侧滑的动画,有多重选择。

  为了监听点击按钮的事件,你有两种方法:

  ■实现MGSwipeTableCellDelegate

  ■定义MGSwipeButton按钮时有方便使用的block回调:

    [MGSwipeButton buttonWithTitle:@"More"
                   backgroundColor:[UIColor lightGrayColor]
                          callback:^BOOL(MGSwipeTableCell *sender) {
                              NSLog(@"Convenience callback for swipe buttons!");
                          }];
Delegate

  MGSwipeTableCellDelegate 是用来配置滑动按钮或接收触发的动作或另一个事件的可选的委托。按钮可以在cell创建的时候就嵌入进去而不用使用delegate,不过使用delegate可以改善内存的使用,因为按钮只有在使用的时候才创建。

- (BOOL)swipeTableCell:(MGSwipeTableCell*)cell canSwipe:(MGSwipeDirection)direction;

  决定是否可以使用划动手势。

- (void)swipeTableCell:(MGSwipeTableCell*)cell
   didChangeSwipeState:(MGSwipeState)state
       gestureIsActive:(BOOL) gestureIsActive;

  当前swipe state状态改变时使用。

- (BOOL)swipeTableCell:(MGSwipeTableCell*)cell
   tappedButtonAtIndex:(NSInteger)index
             direction:(MGSwipeDirection)direction
         fromExpansion:(BOOL) fromExpansion;

  用户点击按钮时回调。

- (NSArray*)swipeTableCell:(MGSwipeTableCell*)cell
  swipeButtonsForDirection:(MGSwipeDirection)direction
             swipeSettings:(MGSwipeSettings*)swipeSettings
         expansionSettings:(MGSwipeExpansionSettings*)expansionSettings;

  设置swipe button 和 swipe/expansion 的设置。

可扩展的按钮(Expandable buttons)

  按钮默认是不可以扩展的。你可以使用cell.leftExpansion 和 cell.rightExpansion 来设置可可扩展的按钮。

  可扩展的按钮事件是当用户完成滑动手势时自动触发的。扩展程度是动态的(通过threshold的值来设置)。触发的可扩展按钮可以恢复到其初始位置或填充整个 UITableViewCell,您可以使用 fillOnTrigger 属性选择所需的动画。

@interface MGSwipeExpansionSettings: NSObject

/** index of the expandable button (in the left or right buttons arrays) */
@property (nonatomic, assign) NSInteger buttonIndex;

/** if true the button fills the cell on trigger, else it bounces back to its initial position */
@property (nonatomic, assign) BOOL fillOnTrigger;

/** Size proportional threshold to trigger the expansion button. Default value 1.5 */
@property (nonatomic, assign) CGFloat threshold;

@end

 

转载于:https://my.oschina.net/fadoudou/blog/736691

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值