侧滑UITableViewCell展现多个可操做按钮是iOS开发中经常使用到的一个功能。这里有个很是强大的开源库:MGSwipeTableCell,能够实现此功能。其效果以下图所示:ios
示意图.gifgit
■CocoaPods 引入
建议使用,没有使用过CocoaPods的童鞋能够参照大神唐巧的这篇文章用CocoaPods作iOS程序的依赖管理。github
■手动引入
下载地址MGSwipeTableCell。数组
能够直接用MGSwipeTableCellcell,也能够继承于MGSwipeTableCell,下面是一个使用的例子:app
- (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是侧滑的动画,有多重选择。atom
为了监听点击按钮的事件,你有两种方法:spa
■实现MGSwipeTableCellDelegatecode
■定义MGSwipeButton按钮时有方便使用的block回调:blog
[MGSwipeButton buttonWithTitle:@"More" backgroundColor:[UIColor lightGrayColor] callback:^BOOL(MGSwipeTableCell *sender) { NSLog(@"Convenience callback for swipe buttons!"); }];
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 的设置。
按钮默认是不能够扩展的。你能够使用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