iOS UITableView 开启预估高度, 列表闪烁,向上或者向下位移

当我们开启预估高度,cell中约束做好,那么cell就会自动适配内容大小,很方便,但也有问题,列表闪烁,向上或者向下位移。

- (void)viewDidLoad {
//开启预估行高
    //_tableview.estimatedRowHeight = 0;  先注掉,
    _tableview.rowHeight = UITableViewAutomaticDimension;
//我的headerView和footView都是自定义的,没拉约束,所以就关掉下面两个
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
}


//我是多分区的,所以设置下分区的cell预估高度,尽可能准确
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.section == 0) {
            return 75*myscale;
        }else if(indexPath.section == 1){

        return 300*myscale;
        }else if (indexPath.section == 2){
            return 224*myscale;
        }else if (indexPath.section == 3){
            return 224*myscale;
        }
        return 140*myscale;
}

这样设置的时候,能解决一部分问题,但还不能完全解决。
当我没办法的时候,灵光一现,我是不是应该先把首页所有的请求都完成以后在刷新页面。
果断使用信号量
  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
       [self hongkongEliteAgent:semaphore];
    });
    
   
     dispatch_group_async(group, queue, ^{
        [self wonderfulQuestion:semaphore];
       
    });
    

    dispatch_group_async(group, queue, ^{
        [self getBannerInfo:semaphore];
  
    });
    
    
    dispatch_group_async(group, queue, ^{
        [self columnArticle:semaphore];
    
    });
    
    dispatch_group_async(group, queue, ^{
        [self getProductRecommendList:semaphore];
      
    });
    
    dispatch_group_async(group, queue, ^{
        [self getNewsList:semaphore];
        
    });
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        dispatch_group_notify(group, queue, ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"信号量为0");
            dispatch_async(dispatch_get_main_queue(), ^{
                // 通知主线程刷新 刷新
                [self.tableview reloadData];

                
            });
            
        });
    });
  }


就这样,页面整齐已经ok了,不会闪烁了,真高兴,然而我发现当我点击我第2个分区的cell,再返回来,列表向下滚动。我就服了,差点砸电脑,想了想,赔不起,算了。
后来实在没办法,我就开始去把第二分区的cell换成第三分区的,没啥用,排除cell的问题。
然后我去去掉headerView和footerView,发现正常了,然后我一个个查,发现是第一个分区footerView的锅。
然后我就试着把footerview,改成headerView,果然成功了,虽然我不知道,这是为啥,只能过后慢慢去理解,但至少问题解决了呀,真高兴。

所以我决定以后如高要使用预估高度,我就不写footerView,就这么任性。哪位大佬知道,求赐教

 

在我们不需要预估高度的UITableview的时候,页面会闪烁,这时候就需要我们关掉默认的预估行高了

   _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;

 

解决问题的时候,还用了几个方法

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    NSLog(@"头部section===%ld,height===%f",section,view.frame.size.height);
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(nonnull UIView *)view forSection:(NSInteger)section{
     NSLog(@"底部section===%ld,height===%f",section,view.frame.size.height);
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
   // [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
     NSLog(@"cell的section===%ld,height===%f",indexPath.section,cell.frame.size.height);
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值