我在 UITableViewCell
上平滑滚动时遇到问题。
当我在 UITableView
中缓慢滚动时,我得到 30-40 FPS 并且我的单元格在跳跃(首先在顶部,最后在底部)。
当我快速滚动时,我得到 55-60 FPS,细胞没有跳跃,一切都很好。
FPS 由 Profile 测量。
此外,当我滚动时,CPU 使用率约为 15-25%
我正在使用 UIStoryboard
创建单元格、Realm DB
和 UITableViewAutomaticDimension
这是我的 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
}
}
也许我必须创建 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/