对于每个UIImageView,我想添加标签子视图到它。 这是我的类继承的形式的UIImageViewObjective-C:添加子视图只适用于一个视图
-(instancetype)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame]) {
self.categoryLabel=[[UILabel alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 50)];
self.categoryLabel.textAlignment=NSTextAlignmentCenter;
self.categoryLabel.font=[UIFont systemFontOfSize:20];
self.categoryLabel.textColor=[UIColor whiteColor];
[self addSubview:self.categoryLabel];
NSLog(@"%@",self.subviews);
}
return self;
}
-(void)setModel:(HorizontalModel *)model
{
_model=model;
self.categoryLabel.text=self.model.category;
[self sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"XXXXX%@",self.model.imgURL]] placeholderImage:[UIImage imageNamed:@"obama"]];
}
这是我的视图中的控制器的代码。
-(void)addImage:(NSNotification *)notification
{
self.HArrayLists=notification.userInfo[@"array"];
for (int i=0; i<[self.HArrayLists count]; i++) {
JTImageView *imageView=[[JTImageView alloc] initWithFrame:CGRectMake(i*310, 0, 300, 200)];
imageView.model=[HorizontalModel restaurantsDetailWithDict: self.HArrayLists[i]];
[self.mediaScrollView addSubview:imageView];
}
self.mediaScrollView.contentSize=CGSizeMake(310*[self.HArrayLists count], 0);
}
事实证明,只有第一个imageView显示一个标签,而其余的imageViews只显示图像。
您确定HArrayLists中的所有数据都包含所有条目的文本吗?只要调试,你可以这样做:imageView.categoryLabel.text = @“Test”;并注释掉imageView.model =部分。很可能你的数据是@“”,所以没有什么可以显示 – GeneCode
赞同Rocotilos;也考虑使用self.HArrayLists的-enumerateObjectsUsingBlock方法;它看起来更清洁 \t [self.HArrayLists enumerateObjectsUsingBlock:^(HArrayListsInst * OBJ,NSUInteger IDX,BOOL *停止){ \t}]; –
为什么你看到self.mediaScrollView高到0? –