0

当我创建一个新的UITableView时,我可以设置 cell.imageView。理论上,这不是应该显示图像吗?是在 a 中实际显示图像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)
    }
4

3 回答 3

3

你应该让一个单元出队而不是每次都分配一个新的:

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 以验证问题不在于实践数组中的徽标。

于 2018-09-16T13:25:49.403 回答
1

在单元格中实际显示图像的唯一方法是创建一个服装单元格吗?

不,那不是真的。您也可以按照以下方式设置它:

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)

于 2018-09-16T12:21:39.217 回答
0

请检查:

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
}
于 2018-09-16T15:33:39.080 回答