定位 UITableViewCell 的 imageView [英] Positioning the imageView of a UITableViewCell

查看:15
本文介绍了定位 UITableViewCell 的 imageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UITableView 的每个 UITableViewCell 中,我都会在其 imageView 中放置一个图像.但是,如果我使用 tableView:heightForRowAtIndexPath: 增加单元格的高度以容纳更多文本,我发现:

In every UITableViewCell of a UITableView I'm placing an image in its imageView. However, if I increase the height of a cell using tableView:heightForRowAtIndexPath: to accomodate more text, I've found that:

  1. 图像垂直居中对齐.
  2. 图片的左边距增加.这会将文本向右推,使其左边距不再与上下单元格中的文本对齐.

我希望图像垂直上对齐,以便它位于单元格的左上角(如下图中的第一个单元格)并且其左边距不会增加(这样第二个单元格中文本的左边距与第一个单元格中的文本对齐).

I'd like the image to be top-aligned vertically, so that it is in the upper-left corner of the cell (like the first cell in the image below) and not have its left margin increased (so that the left margin of the text in the second cell aligns with the text in the first cell).

改变 imageView 的框架、中心等似乎没有影响.有任何想法吗?谢谢...

Altering the frame, center, etc. of the imageView seems to have no affect. Any ideas? Thanks...

(我有这个问题的图片,但是 SO 不会让我发布图片,因为我是一个声望不到 10 的菜鸟.Grrr...)

(I have an image of the problem, but SO won't let me post an image yet because I'm a noob with less than 10 reputation. Grrr...)

推荐答案

需要继承 UITableViewCell 并覆盖 layoutSubviews,如下:

You need to subclass UITableViewCell and override layoutSubviews, as follows:

- (void) layoutSubviews
{   
    [super layoutSubviews];

    self.imageView.frame = CGRectMake( 10, 10, 50, 50 ); // your positioning here
}

在您的 cellForRowAtIndexPath: 方法中,请务必返回您的新单元格类型的实例.

In your cellForRowAtIndexPath: method, be sure to return an instance of your new cell type.

这篇关于定位 UITableViewCell 的 imageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆