【发布时间】: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