我正在尝试在iOS应用程序中创建集合视图,其中将显示图像和标签。我能够在集合视图中显示图像,但我遇到UILabel
的问题。我有:
NSArray *categoryArray;
categoryArray = [NSArray arrayWithObjects:@"abc",@"xyz",@"qwe",@"asdf",@"fgh",@"hjk",@"lkj",@"ghj",@"sdf",nil];
。
现在我希望将此文本动态分配给放置在Collection视图中的UILabel
。
这是我尝试过的。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
//UILabel *txtCatName = (UILabel *)[cell viewWithTag:100];
// txtCatName.text = @"Testing";
return cell;
}
答案 0 :(得分:0)
将标签放在UICollectionViewCell
中,然后按照添加图像的方式,动态添加标签文本。将您的功能代码编辑为类似的内容。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
cell.txtLabel.text = [categoryArray objectAtIndex:indexPath.row];
//UILabel *txtCatName = (UILabel *)[cell viewWithTag:100];
// txtCatName.text = @"Testing";
return cell;
}
答案 1 :(得分:0)
更改标签标签并尝试此操作。 (设置标签标签:99)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
UILabel *txtCatName = (UILabel *)[cell viewWithTag:99];
txtCatName.text = [categoryArray objectAtIndex: indexPath.row] ";
return cell;
}
答案 2 :(得分:0)
把这个放在线后
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
/*This is to avoid overlapp due to cell reuse.This will remove all labels in the cell. If only a particular label is to be removed put a tag range to it like label.tag = 100+indexPath.row and change the if accordingly to match this tag range*/
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
[view removeFromSuperview];
}
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];//Set the frame as per your requirement.
[label setText:[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:indexPath.row]]];
[cell addSubview:label];
return cell;
此外,如果您使用自定义单元格,请向单元格添加UILabel
并创建IBOulet
属性,然后更新此方法中的文本。无需担心重叠。最好通过xib添加,因为如果子视图的数量增加,循环遍历所有子视图将导致性能问题。希望这会有所帮助。