自定义UITableViewCell与一个可选的图像

我有一个自定义的UITableView单元格,具有UIImageView大小的单元格的宽度在顶部,并在它下面的UILabel

UITableViewCell有一个imageView属性。 如果图像视图的图像被设置,那么它将单元格textLabel向右推,以适应图像。 我想做一些类似于我的表格视图单元格(除了我的标签将被推下来)。

我怎么能做到这一点?

做一个简单的方法是UITableViewCell并重写layoutSubviews方法。

这里有个简单的例子。

 @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定位单元的组件。

希望能帮助到你。