【问题标题】:loading images from coredata , tableview is not scrolling smooth从 coredata 加载图像,tableview 滚动不流畅
【发布时间】:2020-01-02 13:29:25
【问题描述】:

我有六个自定义单元格。 3 个单元格包含 imageView 和一些标签,而其他只包含标签。在 viewDidLoad 中,我从 coredata 加载了所有数据并刷新了 tableview 问题是当表格有单元格(没有 imageview 单元格)时它会平滑滚动,但是当我们添加单元格(有 imageview 单元格)时,它会在向下滚动时平滑滚动,但是当我在向上滚动表格视图。我试图降低图像质量但没有奏效。如何解决这个问题

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    let insuranceCell = tableView.dequeueReusableCell(withIdentifier: "insuranceCell") as! InsuranceCell
    let pollutionCell = tableView.dequeueReusableCell(withIdentifier: "pollutionCell") as! PollutionCell
    let servicingCell = tableView.dequeueReusableCell(withIdentifier: "servicingCell") as! ServicingCell
    let challanPaidCell = tableView.dequeueReusableCell(withIdentifier: "challanPaidCell") as! ChallanPaidCell
    let insuranceClaimCell = tableView.dequeueReusableCell(withIdentifier: "insuranceClaimCell") as! InsuranceClaimCell
    let fuelRefillCell = tableView.dequeueReusableCell(withIdentifier: "fuelRefillCell") as! FuelRefillCell

    let cellArr = sections[indexPath.section].cell
    //Loading cell based on array details
    switch cellArr[0] {
    case is InsuranceDetails:
        insuranceCell.setup(object: cellArr[indexPath.row])
        return insuranceCell
    case is Pollution:
        pollutionCell.setup(object: cellArr[indexPath.row])
        return pollutionCell
    case is Servicing:
        servicingCell.setup(object: cellArr[indexPath.row])
        return servicingCell
    case is ChallanPaid:
        challanPaidCell.setup(object: cellArr[indexPath.row])
        return challanPaidCell
    case is InsuranceClaims:
        insuranceClaimCell.setup(object: cellArr[indexPath.row])
        return insuranceClaimCell
    case is FuelRefills:
        fuelRefillCell.setup(object: cellArr[indexPath.row])
        return fuelRefillCell
    default:
        return insuranceCell
    }

}


class InsuranceCell: UITableViewCell {

    func setup(object : NSManagedObject){
    guard let arr = object as? InsuranceDetails else {return}
    lbAmountPaid.text = arr.amountPaid
    lbAgency.text = arr.agency
    lbVehiclevalidfrom.text = arr.vehicleValidFrom
    lbVehiclevalidupto.text = arr.vehicleValidUpto
    let imageData = arr.insurancePhoto ?? Data()
    let image = UIImage(data: imageData)
    let compimapge = image?.resized(withPercentage: 0.1)
    insurancePhoto.image = compimapge

}

【问题讨论】:

  • 为什么每次都将所有单元格类型出列?尝试在实际需要时将每个tableView.dequeueReusableCell 移动到开关盒中。 (这可能不是您遇到的唯一问题......)
  • 我觉得卡顿的主要问题是let imageData = arr.insurancePhoto ?? Data() let image = UIImage(data: imageData)这行代码,加载需要一些时间。同样在下一步中,您正在调整图像大小。

标签: ios swift uitableview core-data


【解决方案1】:

正如 Fabio 所说,不要一直不必要地使所有单元格出列。

但是,图像创建和缩放很可能是卡顿的来源。在您对UIImage.resized(withPercentage: 0.1) 的调用中对参数的命名表明您的源图像确实很大(您以原始大小的 1/1000 显示它们!?)。如果参数名称有误导性,而 0.1 真的意味着 1/10,我建议重命名参数(可能是UIImage.resized(withFraction: 0.1))。

说了这么多,看看把图像从主线程移开。像这样的东西(未经测试):

class InsuranceCell: UITableViewCell {

    func setup(object: NSManagedObject, in table: UITableview, at indexPath: IndexPath) {
        guard let arr = object as? InsuranceDetails else {return}

        lbAmountPaid.text = arr.amountPaid
        lbAgency.text = arr.agency
        lbVehiclevalidfrom.text = arr.vehicleValidFrom
        lbVehiclevalidupto.text = arr.vehicleValidUpto

        // Do time consuming stuff in the background
        DispatchQueue.global(qos: .userInitiated).async { 
            let imageData = arr.insurancePhoto ?? Data()
            let image = UIImage(data: imageData)

            // This will be where all the time is going. 0.1% suggests your source
            // image is massively oversized for this usage.
            let compimage = image?.resized(withPercentage: 0.1)

            // Always back to the main thread/queue for UI updates
            DispatchQueue.main.async {
                guard let cell = table.cellForRow(at: indexPath) as? InsuranceCell else {
                    // The cell we wanted to configure is no longer in view
                    return
                }

                // Don't be tempted to write straight to `self.insurancePhoto.image`,
                // it may already have been reused for a different row
                // if the user is scrolling quickly
                cell.insurancePhoto.image = compimage
            }

        }
    }
}

它需要额外的参数来检查缩放完成后更新单元格是否仍然合适。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多