如何更改uitableview单元格内图像的位置

how to change the position of image inside cell of uitableview

我已经将图像添加到 uitableview 的单元格中,但是当我尝试更改图像的位置时没有任何反应。

这是我的表视图函数的样子:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    let article = articles[indexPath.row] as Article

    cell.textLabel!.text = article.title

    cell.imageView?.image = article.image
    let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
    cell.imageView?.frame = rect

    return cell
}

如您所见,图像出现了,但位置保持不变:

知道为什么会这样吗?

  • 创建 UITableViewCell
  • 的子类
  • 确保您的单元格将在 tableView 中使用
  • 覆盖 layoutSubviews

    class MyCell: UITableViewCell {
      override func layoutSubviews() {
        super.layoutSubviews()
    
        self.imageView?.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
      }
    }