MGSwipeTableCell的使用(UItabelCell 滑动拉出多个按钮)

来源:互联网 发布:多功能数据采集器 编辑:程序博客网 时间:2024/04/28 08:01

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

引入工程
■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

冻僵的企鹅(简书作者)
原文链接:http://www.jianshu.com/p/25908a61f849
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

0 0
原创粉丝点击