当我创建一个新的UITableView
时,我可以设置cell.imageView。从理论上讲,是不是应该展示一个形象?是实际在UITableViewCell
中显示图像以创建自定义单元子类的唯一方法吗?
这是我正在使用的代码:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell (style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
cell.textLabel?.text = practices[indexPath.row].name
cell.detailTextLabel?.text = practices[indexPath.row].address?.displayString()
//this doesn't show an image
cell.imageView?.clipsToBounds = true
cell.imageView?.contentMode = .scaleAspectFill
cell.imageView?.image = practices[indexPath.row].logo
return (cell)
}
您应该将单元格出列而不是每次都分配一个单元格:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell
cell.imageView?.image = practices[indexPath.row].logo
return cell
}
正如其他人所建议的那样,在xcassets中添加一个测试图像,以验证问题与实践数组中的徽标无关。
在单元格中实际显示图像的唯一方法是创建服装单元格吗?
不,这不是真的。你也可以按照以下方式设置它:
cell.imageView?.image = some UIImage
在你的代码中
cell.imageView?.image = practices[indexPath.row].logo
请检查practices[indexPath.row].logo
实际上有一个UIImage
另外一个注意事项,使用dequeueReusableCell
let cell = tableView.dequeueReusableCell(withIdentifier: "someCellName", for: indexPath)
而不是每次在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
分配它
请检查:
if practices[indexPath.row].logo is UIImage {
print("My logo is not UIImage")
cell.imageView?.image = nil
} else {
print("My logo is UIImage")
cell.imageView?.image = practices[indexPath.row].logo
}