定位 UITableViewCell 的 imageView
在 UITableView 的每个 UITableViewCell 中,我都将图像放置在其 imageView 中。但是,如果我使用 tableView:heightForRowAtIndexPath: 增加单元格的高度以容纳更多文本,我发现:
- 图像垂直居中对齐。
- 图像的左边距增加。这会将文本向右推,使其左边距不再与上方和下方单元格中的文本对齐。
我希望图像垂直顶部对齐,以便它位于单元格的左上角(如下图中的第一个单元格),并且其左边距不增加(以便第二个单元格中的文本与第一个单元格中的文本对齐)。
改变imageView的框架、中心等似乎没有影响。有什么想法吗?谢谢...
(我有这个问题的图像,但是还不允许我发布图像,因为我是一个声誉低于 10 的菜鸟。Grrrr...)
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:
- The image is center-aligned vertically.
- The left margin of the image increases. This pushes the text right so its left margin is no longer aligned with the text in the cells above and below.
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).
Altering the frame, center, etc. of the imageView seems to have no affect. Any ideas? Thanks...
(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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要子类化 UITableViewCell 并重写 layoutSubviews,如下所示:
在 cellForRowAtIndexPath: 方法中,确保返回新单元格类型的实例。
You need to subclass UITableViewCell and override layoutSubviews, as follows:
In your cellForRowAtIndexPath: method, be sure to return an instance of your new cell type.
当您认为需要调整默认样式中项目的大小或位置时,您就需要考虑创建一个自定义单元格,其中您自己的项目的大小和位置按照您想要的方式进行。这就是您可以创建自定义单元格的原因。 :)
The moment you think you need to adjust sizes or positions of items in the default styles, is the time you need to think about creating a custom cell with your own items sized and positioned the way you want them to be. This is why you can create custom cells. :)
另一种方法(适用于 iOS 6)是创建一个 UIImageView 并将其作为子视图添加到单元格中
Another way (works in iOS 6) is to create a UIImageView and add it to the cell as a subview
您可能没有尝试过的一个非常有用的函数(在
UITableViewDelegate
中)是tableView:willDisplayCell:forRowAtIndexPath:
。它在单元格显示之前立即调用,其目的是使您能够对布局进行操作,而无需操作系统在显示之前再次对其进行修改。实际上,您拥有完全的控制权。我用它来更改单元格中的字体,但您也可以迭代单元格的子视图并修改它们的框架。不过我同意你现在是时候研究自定义单元并停止与操作系统对抗了。这并不难,苹果在 iOS 表格视图编程指南。
A really useful function you might not have tried (in
UITableViewDelegate
) istableView:willDisplayCell:forRowAtIndexPath:
. It is called immediately before a cell is displayed and its purpose is to enable you to do things to the layout without the OS modifying it again before display. Effectively you have total control. I've used it for changing fonts in cells but you can iterate the cell's subviews and modify their frames too.Still I agree you are at the point where it is time to look into custom cells and stop fighting the OS. It isn't hard to do and Apple gives a good example in Table View Programming Guide for iOS.
TomSwift 解决方案的 Swift 4 变体(对我来说效果很好):
Swift 4 variation of TomSwift's solution (which worked for me nicely):
您可以创建自定义单元格并将对象放置在您需要的位置以及设置自动遮罩值。
You can create a custom cell and place the objects where you need them to be as well as setting the auto mask values.
在故事板中使用
CustomCell
代替UITableViewCell
。还从数据源返回 CustomCell 实例,如下所示
Use
CustomCell
in your storyboard insted ofUITableViewCell
.Also return CustomCell instance from datasource as