Ios 从coredata加载图像时,tableview无法平滑滚动

Ios 从coredata加载图像时,tableview无法平滑滚动,ios,swift,uitableview,core-data,Ios,Swift,Uitableview,Core Data,我有六个自定义单元格。3个单元格包含imageView和一些标签,其他单元格仅包含标签。在viewDidLoad中,我加载了coredata中的所有数据并刷新了tableview。问题是,当表格中的单元格没有imageview单元格时,它会平滑地滚动,但当我们添加带有imageview的单元格时,它会平滑地向下滚动,但当我向上滚动tableview时,它会结构化。我试图降低图像质量,但没有成功。如何解决这个问题 func tableView(_ tableView: UITableView, c

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

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

}

正如法比奥所说,不要总是不必要地让所有的单元格出列

然而,图像的创建和缩放很可能是口吃的根源。调用UIImage.resizedwithPercentage:0.1时对参数的命名表明源图像的质量确实很高,显示它们的大小是其原始大小的1/1000!?。如果参数名有误导性,并且0.1实际上意味着十分之一,我建议重命名参数UIImage.resizedwithFraction: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
            }

        }
    }
}

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

为什么每次都要将所有单元格类型出列?在实际需要时,尝试将每个tableView.dequeueReusableCell移动到交换机案例中。这可能不是你唯一的问题…我认为滞后的主要问题是这行代码让imageData=arr.insurancePhoto??Data let image=UIImagedata:imageData,加载它需要一些时间。同样在下一步中,您将调整图像的大小。