带有可选图像的自定义 UITableViewCell [英] Custom UITableViewCell with an optional image
问题描述
我有一个自定义 UITableView
单元格,它有一个 UIImageView
大小为顶部单元格的宽度,下面有一个 UILabel
.
I have a custom UITableView
cell that has a UIImageView
sized to the width of the cell at the top, and a UILabel
underneath it.
UITableViewCell
有一个 imageView
属性.如果设置了图像视图的图像,则它会将单元格 textLabel
推到右侧,以容纳图像.我想对我的表格视图单元做类似的事情(除了我的标签会被推下).
A UITableViewCell
has an imageView
property. If the image view's image is set, then it pushes the cell textLabel
to the right, to accomodate the image. I would like to do something similar with my table view cell (except that my label would be pushed down).
我怎样才能做到这一点?
How could I accomplish this?
推荐答案
一个简单的方法是继承 UITableViewCell
并覆盖 layoutSubviews
方法.
A simple way to do it is to subclass UITableViewCell
and override layoutSubviews
method.
这是一个简单的例子.
@interface CustomCell : UITableViewCell
@end
@implementation CustomCell
- (void)layoutSubviews
{
[super layoutSubviews];
// grab bound for contentView
CGRect contentViewBound = self.contentView.bounds;
if(self.imageView.image) {
// do stuff here, for example put the image right...
CGRect imageViewFrame = self.imageView.frame;
// change x position
imageViewFrame.postion.x = contentViewBound.size.width - imageViewFrame.size.width;
// assign the new frame
self.imageView.frame = imageViewFrame;
}
else {
// do stuff here
}
}
特别是在此方法中,您可以根据 image
为单元格定位组件.
In particular inside this method you can position the components for your cell depending on image
.
希望有帮助.
这篇关于带有可选图像的自定义 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!