UICollectionViewCells重叠

时间:2014-07-03 07:19:48

标签: ios iphone objective-c uicollectionview uicollectionviewcell

我正在使用UICollectionView在我的应用中显示数据。现在问题是当我滚动视图时,有时单元格位置不会保持稳定。它们开始相互重叠。我在下面的方法中检查了我提供的值。他们是对的。这是我的代码:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

我正在使用默认布局

self.collectionView.collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];

我想我注意到UICollectionViewCell没有根据UICollectionView宽度调整大小 ![uicollectionView cell overlap] [2]

正如您在附加图像中看到的那样,集合视图背景颜色(红色)不会出现在完整UICollectionViewCell的背面。需要一些指导。

感谢。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {


UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

cell.backgroundColor = [UIColor whiteColor];

Post *post=[_fetchedResultsController objectAtIndexPath:indexPath];

LoungeUserPostCell *loungepostcell=(LoungeUserPostCell *)cell;

if (indexPath.row==1)
{
    loungepostcell.userPostedText.text=post.chat_msg;

    loungepostcell.lblPostTime.text=@"";
    loungepostcell.lblUsername.text=[NSString stringWithFormat:@"userid =%@", post.user_id];
}
else
{
    if (post.chat_img_medium !=nil || post.chat_img_full!=nil)
    {
        //            CGRect chat_msg_rect=CGRectFromString(post.chat_msg_rect);
        [loungepostcell willHaveImage:YES imageRect:CGRectMake(10, [post.height floatValue]-kPostImageHeight, kPostImageWidth, kPostImageHeight)];

        if (post.chat_img_medium!=nil) {
            [loungepostcell.userPostedImage setImageWithURL:[NSURL URLWithString:post.chat_img_medium] placeholderImage:[UIImage imageNamed:@"G-story.png"]];
        }
        else
            [loungepostcell.userPostedImage setImageWithURL:[NSURL URLWithString:post.chat_img_full] placeholderImage:[UIImage imageNamed:@"G-story.png"]];

    }
    else
    {
        [loungepostcell willHaveImage:NO imageRect:CGRectZero];
    }
    loungepostcell.userPostedText.text=post.chat_msg;
    loungepostcell.lblPostTime.text=[[[Shared instance]dateFormateForLosAngeles]stringFromDate:post.chat_time];
    loungepostcell.lblUsername.text=[NSString stringWithFormat:@"%@", post.user_detail.user_name];
}


return cell;

}

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现问题,问题出在单元格中,我将整个视图添加为子视图,因为我使用的是nib。我这样做是因为我也想使用nib和UICollectionViewCell类。

[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"LoungeUserPostCell" owner:self options:nil]objectAtIndex:0]];

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
       // Initialization code
       self.backgroundColor=[UIColor orangeColor];

       [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"LoungeUserPostCell" owner:self options:nil]objectAtIndex:0]];

       [self onLoadAdjustments];
    }
    return self;
}

现在我在Collection视图中将其注册为nib,并使用- (void)awakeFromNib方法在nib中进行更改。