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

时间:2015-01-30 20:24:11

标签: ios iphone uitableview swift

我已将图像添加到uitableview的单元格中,但当我尝试更改图像的位置时,没有发生任何事情。

这是我的tableview函数的样子:

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
}

正如您所见,图像显示但位置保持不变:

ur.com/1EP0o.png

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

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

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