简体   繁体   English

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

[英]how to change the position of image inside cell of uitableview

I have added an image to the cell of a uitableview, but when I tried to change the position of the image nothing happened. 我已将图像添加到uitableview的单元格中,但是当我尝试更改图像的位置时,没有任何反应。

This is what my tableview function looks like: 这是我的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
}

As you can see the image shows up but the position stays the same: 如您所见,图像出现了,但位置保持不变:

ur.com/1EP0o.png

Any ideas why this happens? 任何想法为什么会发生这种情况?

  • Create a subclass of UITableViewCell 创建UITableViewCell的子类
  • Ensure that your cell will be used in the tableView 确保您的单元格将在tableView中使用
  • Override layoutSubviews 覆盖layoutSubviews

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM