ios - 缓慢滚动时 TableView 滞后

标签 ios swift uitableview smooth-scrolling

我在 UITableViewCell 上平滑滚动时遇到问题。 当我在 UITableView 中缓慢滚动时,我得到 30-40 FPS 并且我的单元格在跳跃(首先在顶部,最后在底部)。

当我快速滚动时,我得到 55-60 FPS,细胞没有跳跃,一切都很好。

FPS 由 Profile 测量。

此外,当我滚动时,CPU 使用率约为 15-25%

我正在使用 UIStoryboard 创建单元格、Realm DBUITableViewAutomaticDimension

这是我的 UITableView 代码:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let stop = getStop(indexPath: indexPath)
    let cell = tableView.dequeueReusableCell(withIdentifier: "stop", for: indexPath) as! StopTableViewCell
    cell.configure(model: stop)
    return cell        
 }

这是单元格代码:

class StopTableViewCell: UITableViewCell {


@IBOutlet weak var status: UILabel!
@IBOutlet weak var number: UILabel!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var name: UnderLinedLabel!
@IBOutlet weak var address: UnderLinedLabel!
@IBOutlet weak var phone: UnderLinedLabel!
@IBOutlet weak var milage: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func prepareForReuse() {
    super.prepareForReuse()
    name.text = nil
    address.text = nil
    phone.text = nil
    milage.text = nil
    number.text =  nil
    date.text = nil
}

func calculateEstimateRoute(to lat: Double, lon: Double) -> String {
    let distance = LocationManager.Instance.actualLocation.distance(from: CLLocation(latitude: lat, longitude: lon))
    let isMetric = NSLocale.current.usesMetricSystem
    let measurment = Measurement(value: distance, unit: UnitLength.meters)
    var estimateRoute = !isMetric ? measurment.converted(to: UnitLength.miles) : measurment.converted(to: UnitLength.kilometers)
    estimateRoute.value = round(estimateRoute.value)
    return "~" + String(describing: estimateRoute)
}

func configure<T>(model: T) {
    if let stop = model as? Stop {
        name.text = stop.name
        address.text = stop.address1 + "\n" + stop.address2
        phone.text = stop.phone
        phone.isHidden = stop.phone.isEmpty
        milage.text = calculateEstimateRoute(to: stop.lat, lon: stop.lng)
        number.text =  "# " + stop.refno
        date.text = stop.departtime
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
    }
}

这是单元 Via Storyboard: enter image description here

也许我必须创建 3 个单元格,一个用于顶部 orangeView,第二个用于中心内容,第三个用于底部按钮 View ?

这里是:

func getStop<S: Stop>(indexPath: IndexPath) -> S {
    switch segmentIndex {
    case 0:
        return stops.ASSIGNED[indexPath.row] as! S
    case 1:
        return stops.UNASSIGNED[indexPath.row] as! S
    default:
        return Stop() as! S
    }
}

在标题中我有 UISegmentedControl

最佳答案

我正在看这个并想知道,calculateEstimateRoute 需要多长时间才能完成?这似乎是最有可能的瓶颈。

我建议您使用 Time Profiler 运行 Instruments 并查看有多少处理器能力专用于该功能。如果它很重要,您可以考虑将该函数推送到后台线程 - 可能使用操作队列,以便可以在 prepareForReuse 中取消调用。我对包含图像的单元格使用了类似的技术,因为加载图像可能很耗时。

关于ios - 缓慢滚动时 TableView 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50511267/

相关文章:

ios - Xcode 6 GM 无法使用 iOS 8 模拟器模拟位置

swift - 如何声明表情符号字符变量并在 Swift 中打印它?

ios - 显示键盘时Swift ios移动文本字段

ios - 如果我快速选择其中一项,如何删除所有数组

ios - Google 自动完成功能调用后崩溃

iOS:点击一个推送通知时,同一应用的其他推送通知消失

ios - 删除 TabGroup 选项卡标题并仅在适用于 iOS 的 Appcelerator 应用程序上显示图标

ios - 使用 HealthKit 结果填充 TableView - Swift

ios viewcontroller 在容器内调用子viewcontroller

iphone - 像在联系人应用程序中一样使用添加照片按钮创建 UITableViewCell