iOS UITableViewCell的”滑动出现多个按钮
2016-06-21 11:21
405 查看
前言: 本篇博客其实就是想介绍tableviewcell滑动的一些”事”, 昨天在逛github的时候看到的还挺有意思的三方库, , 简单用了一下感觉不错, 一作为记录, 二是希望有类似需求的可以得到帮助.
本篇介绍了 iOS5之后(使用三方库) iOS8之后(系统方法)分别的实现方式
效果图 – ios>= 5.0
效果图 – ios>= 8.0
直接使用比较简单 通过代码看一下
添加左边按钮方法
Objective-C
添加右边按钮的方法Objective-C
重用池可以这样写
Objective-C
添加按钮Objective-C
按钮的点击代理方法
Objective-C
你还可以加入我创建技术交流群: 498143780 与我交流. 一起学习, 点个赞鼓励一下.
感兴趣的, 你还可以下载Demo https://github.com/Zhangjingwang1993/SwipeCell.git
本篇介绍了 iOS5之后(使用三方库) iOS8之后(系统方法)分别的实现方式
效果图 – ios>= 5.0
效果图 – ios>= 8.0
MGSwipeTableCell(Github上的三方库)- iOS >= 5.0
直接使用比较简单 通过代码看一下首先签这个协议MGSwipeTableCellDelegate
添加左边按钮方法
Objective-C
1234567 | - (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 |
1 2 3 4 5 6 | - (NSArray *)btnRightCount:(int)count { NSMutableArray *result = [NSMutableArray array]; NSArray *titleArray = @[@"删除", @"标记未读"]; UIColor *color[2] = {[UIColor redColor], [UIColor lightGrayColor]}; for (int i = 0; i |
重用池可以这样写
Objective-C
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; return cell;} |
1 2 3 4 5 6 7 8 9 10 11 12 | -(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]; } } |
按钮的点击代理方法
Objective-C
12345678910111213141516171819202122232425262728 | -(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion{ switch (direction) { case MGSwipeDirectionLeftToRight: { if (index == 0) { NSLog(@"right ------- 0"); }else{ NSLog(@"right ------- 1"); } break; } case MGSwipeDirectionRightToLeft: { 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; } } return YES;} |
iOS8 之后也提供了类似的实现
Objective-C1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | - (NSArray *)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]; } |
微博@夏天是个大人了 欢迎你关注我
你还可以加入我创建技术交流群: 498143780 与我交流. 一起学习, 点个赞鼓励一下.感兴趣的, 你还可以下载Demo https://github.com/Zhangjingwang1993/SwipeCell.git
相关文章推荐
- new Integer(1)和Integer.valueOf(1)的区别
- 一次性移除一个 UIView 所有子视图的方法
- @PathVariable和@RequestParam的区别,@SessionAttributes
- vuejsLearn--- v-for列表渲染
- 百度ueditor 视频编辑问题以及图片跟视频居中报错问题解决
- Android开发编码规范、性能优化、UI优化及开发建议
- hibernate3 NoClassDefFoundError: org/hibernate/engine/query/sql/NativeSQLQueryReturn
- requestFeature() must be called before adding content
- WWDC-UIKit 中协议与值类型编程实战
- Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)--by Markdown
- UIAlertView 多个输入框的使用
- 多线程编程(三)NSOperationQueue
- 1.UNIX环境高级编程学习环境搭建---apue.h,unix---apue.h
- 5款高效的原型设计工具
- java.net.BindException: Cannot assign requested address: JVM_Bind
- Qt quick实现无边框可拖拽风格
- easyui的自定义行工具栏
- UI控件之RadioButton(单选按钮)&Checkbox(复选按钮)
- iOS 设置UITextView显示文本的光标
- UItableViewCell的分割线顶头显示