带有可选图像的自定义UITableViewCell

时间:2012-03-15 13:29:28

标签: ios xcode uitableview

我有一个自定义UITableView单元格,其UIImageView的大小与顶部单元格的宽度相同,下方有一个UILabel

UITableViewCell具有imageView属性。如果设置了图像视图的图像,则它将单元格textLabel推向右侧,以容纳图像。我想用我的表格视图单元格做类似的事情(除了我的标签会被按下)。

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:2)

一种简单的方法是继承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定位单元格的组件。

希望它有所帮助。

相关问题