码迷,mamicode.com
首页 > 其他好文 > 详细

自定义cell的左侧滑动

时间:2016-06-17 23:53:18      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

效果如上图,中间那个白线是一个UIview.

如果不添加中间那根白线,用系统的方法就可以实现,方法如下

 1 -(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
 2     UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
 3                                                                         title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
 4                                                                                                                        [self.datas removeObjectAtIndex:indexPath.row];
 5                                                              [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
 6                                                                          }];
 7 //置顶
 8  UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
 9         [self.datas exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
10         NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
11         [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
12     }];
13     topRowAction.backgroundColor = [UIColor blueColor];
14 
15 
16     
17     UITableViewRowAction *rowActionSec = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
18                                                                             title:@"编辑"    handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
19                                                                                                                                  }];
20     rowActionSec.backgroundColor = [UIColor blueColor];
21     rowAction.backgroundColor = [UIColor blueColor];
22     
23     NSArray *arr = @[rowAction,topRowAction ,rowActionSec];
24     return arr;
25 }

要加上那根白线,或者加其他的子控件,可以在自定义cell的时候,给cell添加一个scrollView,设置scrollView的

contentSize为:CGSizeMake([UIScreen mainScreen].bounds.size.width+101, self.bounds.size.height);

将其他子控件添加到scrollView上.

为什么不将scrollView添加到contenView上呢?在cell上有一个contenView,最右侧还有一个辅助视图,添加到contenView上,有bug,会从辅助视图那里出来.

还有一个bug,就是再用Masonry做布局的时候,不能将布局的代码放在layoutSubviews里面,这个方法子控件的frame有改变的时候,就会调用,用Masonry会重复添加约束.

   self.scrollview = [[UIScrollView alloc]initWithFrame:self.bounds];在第一次创建出来scrollview的时候此时scrollview的块为320,在6s上运行同样是320,需要在layoutSubviews方法里面在重新设置下self.scrollview.frame = self.bounds;

实现左滑

1 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
2     if (self.scrollview.contentOffset.x>50) {
3         self.scrollview.contentOffset = CGPointMake(101, 0);
4     }else{
5         self.scrollview.contentOffset = CGPointZero;
6     }
7 }

如果想要实现跟系统一样的功能,就是在滑动cell的时候,只允许一行cell向左滑,在点击屏幕任意地方cell都会复位和点击cell的时候做其它的事情,比如push.

因为给cell添加了scrollview,cell监听不到自己的代理方法,包括touchBegan方法

因此需要给scrollview添加一个tap手势,同过block回调,可以实现push.如果scrollview上添加了删除和编辑按钮,可以给两个按钮设置不同的tag值,在通过block回调可以实现按钮点击事件.

 1 - (void)btnClick:(UIButton *)sender{
 2     if (sender.tag == 1) {
 3         
 4         if (self.editblock) {
 5             
 6             self.editblock(self);
 7             
 8         }
 9     }else if (sender.tag == 2){
10             
11             if (self.deleteblock) {
12                 
13                 self.deleteblock(self);
14             }
15     }
16     
17 }
18 
19 //编辑按钮
20 - (void)editMyCarsSoucesBlock:(MyCarsSoucesBlock)editBlock {
21 
22     self.editblock = editBlock;
23 }
24 //删除按钮
25 - (void)deleteMyCarsSoucesBlock:(MyCarsSoucesBlock)deleteBlock{
26     
27     self.deleteblock = deleteBlock;
28 }

在控制器里面调cell里面的block方法,在block里面写要实现的代码快就可以了.

要实现只有一行cell左滑,在cell里面写个复位的方法,实现这句代码就行 self.scrollview.contentOffset = CGPointZero;

在控制器里面

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    

    for (NSInteger i = 0; i < self.carDatas.count; i++) {

        @autoreleasepool {

        GCMyCarsSoucesCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

            [cell resetScrollerOffset];//重置的方法resetScrollerOffset

        }

    }

    

}

在此方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

实现

        [cell selectCellBlock:^(GCMyCarsSoucesCell *cell) {

        

            for (NSInteger i = 0; i < self.carDatas.count; i++) {

                @autoreleasepool {

                   

                    cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

                        [cell resetScrollerOffset];

                }

            }

    }];

    还有bug,目前我只能写到这里,真希望能得到大神的指点!

 

自定义cell的左侧滑动

标签:

原文地址:http://www.cnblogs.com/feiyiban588/p/5595360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!