点击cell动态改变cell高度

作者: 童星 | 来源:发表于2016-06-16 19:40 被阅读1592次

    title: 点击cell动态改变cell高度date: 2016-06-16 12:08:29
    tags:

    • TableViewCell
    • 动态改变高度
    • cell伸缩
      categories: iOS知识小结

    应一个群朋友的要求,想实现如下图这种的效果: 效果图1

    问题:点击cell,然后根据cell内部出来的view的高度来动态改变当前的点击的cell的高度。

    解决思路:

    1. 点击cell的时候会弹出一个contentView,根据数据,获取这个contentView的高度。为了实现刷新的动画效果,接下来的cell的高度变化都在
    [tableView beginUpdates];
    // code
    [tableView endUpdates];
    

    上面这个代码块之间执行。

    1. 当contentView以动画的形式出现的时候,先展示contentView,然后再改变cell的高度
    2. 当contentView消失的时候,先将contentView消失,然后再刷新这一行的高度
    3. 在返回cell高度的方法中,根据contentView是否显示而显示不同的高度。

    现在cell高度计算,都会先给一个预估高度,这样代理方法就不会一开始走heightForRowAtIndexPath这个方法,会先走cellForRowAtIndexPath这个方法,这样的话,当cell显示的时候才去调用heightForRowAtIndexPath方法,此时cell已经创建成功了。这样就可以在这个方法里通过cellForRowAtIndexPath来获取cell了,相比从前,计算cell高度简单多了。

    最后简单的模拟了一下不同cell的高度,实现的效果如下图: 效果图2

    核心代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        TestTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        if (!self.isVisiable) {
            _index_arr = arc4random()%5;
            _indexPath_cell = indexPath;
            _self_adaption = YES;
            CGFloat f = [[_Harray objectAtIndex:_index_arr] floatValue];
            UIView *testView = [[UIView alloc]
                                initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, f - 10)];
            testView.backgroundColor = [UIColor colorWithRed:1.000 green:0.971 blue:0.138 alpha:1.000];
            testView.userInteractionEnabled = NO;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:testView.bounds];
            imageView.image = [UIImage imageNamed:@"ab97283425c2ce1c75ccac15a1fed5fd.jpg"];
            [testView addSubview:imageView];
            _testView= testView;
            [cell.contentView addSubview:testView];
            
            [tableView beginUpdates];
            testView.transform = CGAffineTransformMakeScale(1, 0.01);
            
            [UIView animateWithDuration:0.5
                             animations:^{
                                 
                                 testView.transform = CGAffineTransformMakeScale(1, 1);
                                 CGRect oldFrame = cell.frame;
                                 oldFrame.size.height = f;
                                 cell.frame = oldFrame;
    
                             }completion:^(BOOL finish){
                                 _visiable = YES;
    
                             }];
            [tableView endUpdates];
            
        }
        else{
        
            [UIView animateWithDuration:0.5
                             animations:^{
                                 
                                 _testView.transform = CGAffineTransformMakeScale(1, 0.01);
                                 
                             }completion:^(BOOL finish){
                                 [_testView removeFromSuperview];
                                 _visiable = NO;
                                 _self_adaption = NO;
    
                                 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                             }];
        }
    }
    

    demo地址:git传送门

    相关文章

      网友评论

      • 谢谢生活:看起来不错
      • 40c6620494c9:为什么我设置了预估行高,heightForRowAtIndexPath方法取出来的cell 是nil
        40c6620494c9:@偶像MJ 能回复我下么。。谢谢

      本文标题:点击cell动态改变cell高度

      本文链接:https://www.haomeiwen.com/subject/vhnmdttx.html