您能帮我找一个解决方案而不是一次又一次地复制相同的代码吗?现在我有了这段代码
$ lsof | grep "outputfile"
如你所见,我一次又一次地复制这部分
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: productImageCell, for: indexPath) as! ProductImageCell
if let product = self.product {
cell.product = product
}
cell.selectionStyle = .none
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: productDetailCell, for: indexPath) as! ProductDetailCell
cell.backgroundColor = UIColor.lightGray
if let product = self.product {
cell.product = product
}
cell.selectionStyle = .none
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: productDeliveryCell, for: indexPath) as! ProductDeliveryTimeCell
cell.selectionStyle = .none
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: productDeliveryCell, for: indexPath) as! ProductDeliveryTimeCell
cell.selectionStyle = .none
return cell
}
}
我确实尝试了类似的东西,但它无法正常工作
let cell = tableView.dequeueReusableCell(withIdentifier: productImageCell, for: indexPath) as! ProductImageCell
if let product = self.product {
cell.product = product
}
cell.selectionStyle = .none
return cell
答案 0 :(得分:1)
在所有单元格中包含product
属性。删除indexpath.row == 2
块,它什么都不做。
写这样的代码,
if indexPath.row == 1 {
var cell = tableView.dequeueReusableCell(withIdentifier: productImageCell, for: indexPath) as! ProductImageCell
cell.product == "" //clear product field for reusing property
if let product = self.product {
cell.product = product
}
cell.selectionStyle = .none
return cell
} else if .....
答案 1 :(得分:0)
//您需要在多个单元格中使用相同的类,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:HomeCell!
if homeArray[indexPath.row]["type"] == "1" {
cell = tableView.dequeueReusableCell(withIdentifier: "ProductTitleCell", for: indexPath) as! HomeCell
}
else if homeArray[indexPath.row]["type"] == "2" {
cell = tableView.dequeueReusableCell(withIdentifier: "ProductImageCell", for: indexPath) as! HomeCell
}
cell.lbl_Title_1.text = homeArray[indexPath.row]["product_name"]
if cell.tag == 2 {
cell.imageview_1.image = UIImage(named: homeArray[indexPath.row]["product_image"]!)
}
return cell
}