[英]Adding Label on UICollectionView Cell at different positions iOS
我正在使用一個iOS應用程序,在該應用程序中,我必須通過在每個圖像上添加一個標簽來在收藏夾視圖中顯示圖像,就像在Facebook中一樣,當我們單擊Facebook中的收藏夾中的圖像時,它將展開其圖像並以標簽。 不同的圖像尺寸會出現不同的尺寸。 我也一樣。 一切正常,但是當我添加小圖像時,它會更改標簽在單元格上的位置。這意味着在大圖像上會出現兩個標簽,一個標簽恰好在我想要的位置,第二個小圖像標簽在我想要的位置。 我正在使用此代碼:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
int i;
Cell *cell;
UILabel * lblName ;
UILabel * lblName1 ;
cell=[collectionView dequeueReusableCellWithReuseIdentifier:REUSEABLE_CELL_IDENTITY forIndexPath:indexPath];
@try
{
NSLog(@"index path is %ld",(long)indexPath.row);
if(rearr.count == 0)
{
cell.imageviews.image=NULL;
}
else
{
NSLog(@"count %lu",(unsigned long)rearr.count);
for (i=0;i<rearr.count; i++)
{
NSLog(@"count %lu",(unsigned long)rearr.count);
image = [UIImage imageWithContentsOfFile:rearr[i]];
NSLog(@"image.size.width1 : %f",image.size.width);
NSLog(@"image.size.height1 : %f",image.size.height);
if(image.size.width > image.size.height)
{
lblName = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 230.0, 300.0,80)];
lblName.text = @"Image";
lblName.textAlignment = NSTextAlignmentCenter;
[lblName setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:10]];
[lblName setTextColor:[UIColor colorWithRed:137.0f/255.0 green:137.0f/255.0 blue:137.0f/255.0 alpha:1.0]];
[lblName setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:lblName];
}
else
{
lblName1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)];
lblName1.text = @"Image1";
lblName1.textAlignment = NSTextAlignmentCenter;
[lblName1 setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:10]];
[lblName1 setTextColor:[UIColor colorWithRed:137.0f/255.0 green:137.0f/255.0 blue:137.0f/255.0 alpha:1.0]];
[lblName1 setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:lblName1];
}
if(image != nil)
{
[itms addObject:image];
}
else
{
NSData *data = [[NSData alloc]initWithContentsOfFile:rearr[i]];
image=[UIImage imageWithData:data];
[itms addObject:image];
}
}
cell.imageviews.image=[itms objectAtIndex:indexPath.row];
cell.imageviews.layer.cornerRadius=5.0f;
cell.imageviews.clipsToBounds = YES;
}
return cell;
}
@catch (NSException *exception)
{
NSLog(@"image insertion in collecvcontroller : exception %@",exception);
return cell;
}
}
請幫幫我。 我不知道我在做什么錯。
當您使用dequeueReusableCellWithReuseIdentifier
為什么每次都要添加UILabel
。 執行以下操作,您將看到期望的輸出。
if(image.size.width > image.size.height)
{
[[[cell contentView] viewWithTag:2] removeFromSuperView];
lblName = [[cell contentView] viewWithTag:1];
if(! lblName)
{
lblName = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)];
[lblName setTag:1];
[[cell contentView] addSubview:lblName];
}
// Do other stuff here
}
else
{
[[[cell contentView] viewWithTag:1] removeFromSuperView];
lblName1 = [[cell contentView] viewWithTag:2];
if(! lblName1)
{
lblName1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)];
[lblName1 setTag:2];
[[cell contentView] addSubview:lblName1];
}
// Do other stuff here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.