平常中所用的tableViewCell,左滑后,编辑状态只有一个删除按钮,现在很多APP上面都自定义实现了cell侧滑自定义编辑btn,比如qq,微信侧滑后有“消息置顶”、“标记为未读”等。
之前在cocoachina上看到SWTableViewCell,效果写的很好,可以支持左滑和右滑自定义btn,当时只是收藏了,留着以后备用。后来在玩手机qq的时候发现这个玩意蛮实用,于是想自己写一个试试看。于是自己按照理解实现了和qq的cell侧滑类似的效果。
原理很简单,就是把自定义的btn在cell初始化的时候传给cell(重写的初始化方法)。然后把btn按顺序布置在cell的右端(这个没什么问题)。最后自定义一个SCContentView继承于UIView,然后覆盖在btn上面,添加pangesture手势,根据手势的滑动和SCContentView的状态来进行判断,简单的实现动画效果。
cell的侧滑效果如图所示
安装方法:
I 下载zip压缩包,把SCSwipeTableViewCell文件夹拖到自己工程下面
import SCSwipeTableViewCell.h
在tableViewCell的delegate里面实现自定义cell的侧滑btn
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 55)]; btn1.backgroundColor = [UIColor redColor]; [btn1 setTitle:@"delete" forState:UIControlStateNormal]; UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 55)]; btn2.backgroundColor = [UIColor greenColor]; [btn2 setTitle:@"add" forState:UIControlStateNormal]; btnArr = [[NSMutableArray alloc]initWithObjects:btn1,btn2, nil];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20, 55)]; label.text = [NSString stringWithFormat:@"swipeCell test row %ld",(long)indexPath.row];
static NSString *cellIdentifier = @"Cell"; SCSwipeTableViewCell *cell = (SCSwipeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[SCSwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell" withBtns:btnArr tableView:_tableView]; cell.delegate = self; }
[cell.SCContentView addSubview:label]; return cell; }
II 在pod 上search SCSwipeTableViewCell直接安装
不足之处还请大家多多指教!
Github 下载地址:https://github.com/MonkeyS914/SCSwipeTableViewCell