滚动时TableView会延迟

时间:2016-11-01 07:29:21

标签: ios objective-c uitableview tableview

当用户滚动时,我的TableView会延迟加载单元格。
我发现了类似的问题,但是这些问题中的人使用图像或下载内容,或者有100多个单元格。
在我的情况下,我有四种不同的一个tableview中的单元格类型(有问题吗?),其中一个在加载新的单元格之前有各种计数和延迟。

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

    if (indexPath.section == 0) {

        UITableViewCell *tvCell = [tableView dequeueReusableCellWithIdentifier:identifierUpper];

        if (!tvCell) tvCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierUpper];

        tvCell.selectionStyle = UITableViewCellSelectionStyleNone;
        _collectionMainInfoView.$width = self.view.$width;
        _collectionMainInfoView.$height = self.view.$height*0.45;

        [tvCell.contentView addSubview:_collectionMainInfoView];
        [tvCell.contentView setBackgroundColor:[UIColor clearColor]];
        [tvCell setBackgroundColor:[UIColor clearColor]];

        return tvCell;
    }

    if (indexPath.row == 0) {

        EventsCell *eventCell = [tableView dequeueReusableCellWithIdentifier:identifierEvent];
        [eventCell setEvent:[Event getNextEvent]];
        return eventCell;
    }

    if (indexPath.row == soldiers.count + 1) {

        InviteOtherCell *cellInvite = [tableView dequeueReusableCellWithIdentifier:identifierInvite];
        return cellInvite;
    }

    else {

        ProfilesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.curSoldier = _curSoldier;
        cell.delegate = self;
//        [cell configureCellWithSoldier:soldiers[indexPath.row - 1]];
        [cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:120];
        return cell;
    }
}

最后一个(在"之后{")在显示之前有延迟

#ProfilesCell.m

- (void)awakeFromNib {
    [super awakeFromNib];

    _cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    _cardView.layer.shadowOffset = CGSizeMake(1,1);
    _cardView.layer.shadowRadius = 2;
    _cardView.layer.shadowOpacity = 0.2;
}

4 个答案:

答案 0 :(得分:0)

这段代码对我来说有点乱。

1)您已经定义了一些视图_collectionMainInfoView,它将被添加到第一部分中每个出列的单元格中,因此您不仅会保留该视图,还会在每次此委托时将其读取到表格视图单元格中方法被调用。 我建议只创建自己的包含主要信息视图的单元格子类。 之后,您将能够为您的表视图注册此UITableViewCell的子类,因此您将摆脱此代码

if (!tvCell) tvCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierUpper];
  • 不要在VC中保留_collectionMainInfoView。闻起来。当您在第一部分中有多个单元格时,如何重用此视图?你最终可能会在几个单元格中引用“跳跃视图”。

2)我们没有看到您ProfilesCell的实施,因此很难说出现了什么可能出错。如果可能,尝试直接在其init中设置单元格的视图层次结构。如果那是不可能的。每次调用此委托方法时都不要生成按钮[self rightButtons]。 您也没有遵循Objective-C代码指南。试着通过他们。

例如在这里:

[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:120];

您的方法参数应以小写

开头

[cell setRightUtilityButtons:[self rightButtons] withButtonWidth:120];

3)为了更好的可读性,我在控制流中使用switch而不是if语句

答案 1 :(得分:0)

UITableView滚动不平滑的唯一原因是您在加载Cell时在主线程中执行long async。试着找出哪些代码可以在异步中长期执行? 尝试评论你的2行(仅供测试)。你在那2 set函数中做了什么?

cell.curSoldier = _curSoldier;

[eventCell setEvent:[Event getNextEvent]];

答案 2 :(得分:0)

滚动不平滑时,通常在显示单元格时执行异步工作。根据你的代码,我怀疑有2行:

1。     cell.delegate = self; 不确定你想在这做什么。它可能会在引用中引起循环?

2。 有时,编译器中的新优化规则会出现命令中的命令问题。就像在你的陈述中一样:

[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:120];

您可以尝试在2个单独的语句和新变量中解决此问题。

答案 3 :(得分:0)

与您的tableview代码无关,但创建阴影会导致性能下降,尤其是在创建很多时。我建议使用shadowPath属性,它会大大加快您的代码速度。

相关问题