【问题标题】:UITableView lags when scrolling with multiple cell types使用多种单元格类型滚动时 UITableView 滞后
【发布时间】:2018-06-27 16:30:37
【问题描述】:

我知道关于这个主题有很多问题,但请耐心等待,因为我没有找到任何解决我的具体问题的方法。

首先,我的整个应用都在代码中(没有情节提要)。我正在创建一个具有多种单元格类型的 tableView(我从后端获取数据)。单元格的顺序不变,这意味着第一个单元格将始终与任何其他第一个单元格的类型相同(不确定您是否明白我的意思,但从技术上讲,单元格类型对于特定的 indexPath 是恒定的。

所以问题是我有方法可以将每个 indexPath 的特定单元格出列。在我的一个单元格中,我有一个 MapboxStatic 地图,它从 Mapbox 的服务器返回地图的图像。假设单元格在初始运行时不在屏幕上,如果我滚动得足够快,tableView 将等待一秒钟左右以加载返回的图像(我尝试在主队列上异步加载它仍然没有任何变化)。

现在我的另一个问题是 UberRides 按钮,它在滚动时也会滞后(同样的问题)。

值得一提的是,如果我在 tableView 中滚动一次(有滞后),然后上下滚动它就不再滞后,但我想这是因为出队的单元格仍在内存中。

如果您需要任何其他详细信息,请发表评论。

[编辑]代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 1 {
        return reserveTableViewCell(at: indexPath)
    } else if indexPath.section == 2 {
        return dateTimeTableViewCell(at: indexPath)
    } else if indexPath.section == 3 {
        return descriptionTableViewCell(at: indexPath)
    } else if indexPath.section == 4 {
        if indexPath.row == 0 {
            return mapTableViewCell(at: indexPath)
        } else {
            return uberTableViewCell(at: indexPath)
        }
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: defaultCell, for: indexPath)
        return cell
    }
}

private func reserveTableViewCell(at indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reserveCell, for: indexPath) as! ReserveTableViewCell
    return cell
}

private func descriptionTableViewCell(at indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: descriptionCell, for: indexPath) as! DescriptionTableViewCell
    return cell
}

private func dateTimeTableViewCell(at indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: dateTimeCell, for: indexPath) as! DateTimeTableViewCell
    return cell
}

private func mapTableViewCell(at indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: mapCell) as! MapTableViewCell
    cell.detailController = self
    return cell
}

private func uberTableViewCell(at indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: uberCell, for: indexPath) as! UberTableViewCell
    return cell
}

最好的

【问题讨论】:

  • 我认为你应该发布一些你的cellForRowAtIndexPath 代码
  • 好的,我刚刚添加了它
  • 你的图片缓存了吗?
  • 它们在第一次加载后被缓存,但它们是位置图的图像,所以不可能全部缓存。

标签: ios swift uitableview mapbox uber-api


【解决方案1】:

嗯,创建细胞很慢。现在知道什么需要这么多时间,很难给你任何建议。使用time profiler 确定有问题的代码。

【讨论】:

    【解决方案2】:

    如果您不需要叠加层,您可以尝试使用MGLMapSnapshotterHere 是 MGLMapSnapshot 如何与表视图一起使用的示例。

    【讨论】:

    • 我已经在使用 Mapbox Static,它也会返回一个快照。但我注意到滚动是滞后的,因为没有加载图像就无法显示单元格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多