Ios 带有可选图像的自定义UITableViewCell
我有一个自定义的Ios 带有可选图像的自定义UITableViewCell,ios,xcode,uitableview,Ios,Xcode,Uitableview,我有一个自定义的UITableView单元格,其顶部有一个大小与单元格宽度相当的UIImageView,下面有一个UILabel UITableViewCell具有imageView属性。如果设置了图像视图的图像,则它会向右推动单元格textlab,以容纳图像。我想对我的表视图单元格做一些类似的事情(除了我的标签会被向下推) 如何实现这一点?一种简单的方法是将UITableViewCell子类化并重写layoutSubviews方法 这里有一个简单的例子 @interface CustomCel
UITableView
单元格,其顶部有一个大小与单元格宽度相当的UIImageView
,下面有一个UILabel
UITableViewCell
具有imageView
属性。如果设置了图像视图的图像,则它会向右推动单元格textlab
,以容纳图像。我想对我的表视图单元格做一些类似的事情(除了我的标签会被向下推)
如何实现这一点?一种简单的方法是将
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
}
}
特别是在该方法中,您可以根据图像
定位单元格的组件
希望能有所帮助。是的,我只是在尝试这方面的一些东西,很高兴能得到一些保证,我走上了正确的道路。谢谢