iOS

iOS类似QQ分组以及遇到错误解决过程

2018-10-19  本文已影响0人  boboliu123

我相信大家在开发的过程中,都做过类似QQ分组的功能。这次我就说一下我在做这个功能的时候所遇到的问题。
先解释一下这个功能是如何做的,不解释直接上代码:

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource; //数据源
@end

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        //初始化数据源 初始化3分组数据
        for (int i = 0; i < 3; i++) {
            NSMutableArray *subArr = [NSMutableArray array];
            [_dataSource addObject:subArr];
        }
    }
    return _dataSource;
}

创建一个tableView和一个数据源,初始化数据源创建了3个分组。接着上关键性的代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MyTableViewHeaderView *myView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([MyTableViewHeaderView class])];
    myView.titleLabel.text = [NSString stringWithFormat:@"当前处的位置section:%ld", section];
    //点击整个section,创建cell数据,然后刷新section,把添加的cell数据展示出来
    [myView clickTap:^{
        NSMutableArray *subArr = self.dataSource[section];
        if (subArr.count > 0) {
            [subArr removeAllObjects];
        } else {
            //创建1-10随机数
            NSInteger random = 1 +  (arc4random() % 3);
            //往子数组中添加1-3个cell
            for (int i = 0; i < random; i++) {
                [subArr addObject:@""];
            }
        }
        
        //刷新tableView
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    }];
    return myView;
}
创建一个sectionHeader,并给这个header附上点击事件。在点击事件内部处理数据。我在这里判断subArr中的内部对象个数,如果有值表示当前section是展开的,没有值是关闭的。当是关闭状态的时候。为subArr添加随机1-3个对象,刷新这个section时候会展示出1-3个cell。代码写完了,高高兴兴的运行程序,附上效果图: Untitled.gif 很高兴功能正常,实现的很完美。到这里本来就结束了,但是我突然想到如果是多个怎么办?我就立马修改数据源进行了尝试。把数据源改成了100个分组。这就出现了问题,在展开和关闭分组的过程中,tableView竟然出现了闪动、偏移的现象。附上效果图: 100个分组.gif 突然就蒙蔽了这个是什么原因造成的,首先我就想到了是偏移量造成的原因,那我自己来计算偏移量解决这样总可以吧。
第一种解决方案附上代码:
//在刷新当前section数据之后计算当前section的偏移量,重置tableView的contentOffset
    CGFloat offset = 0;
    for (int i = 0; i < section; i++) {
        NSArray *tempArr = self.dataSource[i];
        if (tempArr.count > 0 && i != section) { //如果有cell  加上cell的高度
            offset += (tempArr.count * 44);
        }
        
        //加上section的高度
        offset += 60;
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    self.tableView.contentOffset = CGPointMake(0, offset);

出来的效果依然不对,还是会出现闪动、偏移的现象。我通过打印tableView对象,看到他里面的不仅contentOffset会变动,contentSize也会发生变化,所以第一种方案已失败告终。然后我就想到了第二种方式:在section刷新之后重绘tableView,让contentSize固定下来,然后通过计算刷新之前的偏移量与点击section位于tableView的位置差值,然后重新赋值到tableView的contentOffset中。
第二种解决方案附上代码:

//第二种解决方案:
    //首先在刷新之前获取到当前点击section位置与当前tableView偏移量的差值,这个差值再刷新之后应该是不变的,
    //再刷新之后我先获取到当前section的位置信息,然后减去之前的差值,求取出当前tableView的偏移量
    CGRect beforeSectionRect = [self.tableView rectForSection:section];
    //当前section的偏移量
    CGFloat offset = beforeSectionRect.origin.y - self.tableView.contentOffset.y;
    NSLog(@"之前:%@=========%@", NSStringFromCGRect([self.tableView rectForSection:section]), self.tableView);
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView layoutIfNeeded];
    [self.tableView layoutSubviews];
    
    CGRect afterSectionRect = [self.tableView rectForSection:section];
    CGPoint point = CGPointMake(0, afterSectionRect.origin.y-offset);
    self.tableView.contentOffset = point;
    NSLog(@"之后:%@=========%@", NSStringFromCGRect([self.tableView rectForSection:section]), self.tableView);
这次偏移量的问题解决了,但是视图还是会闪动一下,没有达到预期的效果。附上效果图: 第二种解决方案.gif

看来重置偏移量这种办法是不行的,我就去查找资料看一些官方文档,最终找到了问题所在:因为iOS 11后系统默认开启Self-Sizing,Self-Sizing的官方文档是这样解释的:大概就是说我们不用再自己去计算cell的高度了,只要设置好这两个属性,约束好布局,系统会自动计算好cell的高度。 iOS11以后,Self-Sizing默认开启,包括Headers, footers。如果项目中没使用estimatedRowHeight属性,在iOS11下会有奇奇怪怪的现象,因为iOS11之前,estimatedRowHeight默认为0,Self-Sizing自动打开后,contentSize和contentOffset都可能发生改变。

我就用模拟器尝试了一下在9.1系统下面运行果然是没有问题的。 最终解决方案.gif
在iOS11之后的版本中需要设置tableView属性estimatedRowHeight、estimatedSectionHeaderHeight、estimatedSectionFooterHeight默认为0
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor lightGrayColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        [_tableView registerClass:[MyTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([MyTableViewHeaderView class])];
    }
    return _tableView;
}

具体代码地址:https://github.com/liuxiaoxin369/QQGroupListDemo

上一篇下一篇

猜你喜欢

热点阅读