MGSwipeTableCell的使用

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


示意图.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

end.Have fun~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 你同样已经学了不少关于这个 Cell 如何工作的知识;亦即,那个UITableViewCellScrollView...
    Obsession丶执阅读 7,263评论 1 11
  • 突然又难过了,说好不难过的。说好要温暖,要达观,要超脱,要热爱。可是突然间发现自己做不到,把这么好的爱给你,值得吗...
    更向远行阅读 1,833评论 0 0
  • 过去在菜市场有个女的卖肉,基本是买多少钱的肉,她就给割多少钱的,态度和蔼,刀法准。 后来她对象在那里卖,就感觉不一...
    飘雪的季节阅读 1,306评论 0 0
  • 我推开门 屋外是白茫茫的一片 没有边际 偶尔会想起你 堆叠了许久的话语 却无从说起 于是 我尝试着 给自己写信 几...
    羊儿小姐阅读 2,915评论 4 2