gpt4 book ai didi

ios - 带有可选图像的自定义 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 11:15:21 24 4
gpt4 key购买 nike

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

UITableViewCell 有一个 imageView 属性。如果设置了 ImageView 的图像,那么它会将单元格 textLabel 推到右侧,以容纳图像。我想对我的表格 View 单元格做一些类似的事情(除了我的标签会被向下推)。

我怎样才能做到这一点?

最佳答案

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

希望对您有所帮助。

关于ios - 带有可选图像的自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9720684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com