无法重用uicollectionviewcell

时间:2014-07-21 13:32:57

标签: ios uicollectionview uicollectionviewcell

我有一个集合视图,我想在其dequereusablecellwithidentifier方法中使用cellforrowatindexpath。这就是我如何做到这一点,但我得到空单元格作为回报。我试图在没有deque方法的情况下做到这一点,但后来我遇到了在调用reloadData方法时获取重复项的问题:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

cell.backgroundColor = [UIColor colorWithRed:127/225.0 green:140/225.0 blue:141/225.0 alpha:1.0f];
cell.layer.contentsScale = [UIScreen mainScreen].scale;

if (cell == nil)
{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    // Border
    borderTop.frame = CGRectMake(0, 0, cell.frame.size.width, 1.2);
    borderTop.backgroundColor = [UIColor colorWithRed:7/255.0f green:100/255.0f blue:177/255.0f alpha:1.0f];
    borderTop.tag = 10;
    [cell.contentView addSubview:borderTop];

    // Photo
    cellImageView = [UIImageView new];
    cellImageView.tag = 20;
    cellImageView.frame = CGRectMake(0, 90, 145, 95);
    [cell.contentView addSubview:cellImageView];

    // Title
    CellTitleLabel = [UILabel new];
    CellTitleLabel.tag = 30;
    CellTitleLabel.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:CellTitleLabel];

    // Activity
    activityIndicator = [UIActivityIndicatorView new];
    activityIndicator.frame = CGRectMake(60, 35, 30, 30);
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.tag = 40;
    [cellImageView addSubview:activityIndicator];
}

borderTop = (UIView *) [cell.contentView viewWithTag:10];
CellTitleLabel = (UILabel *) [cell.contentView viewWithTag:30];
cellImageView = (UIImageView *) [cell.contentView viewWithTag:20];
activityIndicator = (UIActivityIndicatorView *) [cell.contentView viewWithTag:40];

// Title
[CustomLabel DynamicLabel:CellTitleLabel textInput:[[popularCategory TitleArray] objectAtIndex:indexPath.row] fontContent:[UIFont fontWithName:@"Helvetica" size:14] originX:5 originY:3 originW:135];

// Thumbnail
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[popularCategory.ThumbnailArray objectAtIndex:indexPath.row]]];

[activityIndicator startAnimating];

[cellImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
     [activityIndicator stopAnimating];
     [cellImageView setImage:image];

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
    NSLog(@"fail");

    UIImage *failImg = [UIImage imageNamed:@"PlaceholderCell"];
    [cellImageView setImage:failImg];
    [activityIndicator stopAnimating];
}];

return cell;
}

1 个答案:

答案 0 :(得分:0)

如果您已注册该类,则dequeue方法将永远不会返回nil单元格,因此您的if(cell == nil)子句将不会执行。您在该子句中的所有代码(出队行除外)应该在单元格的init方法中。除非您添加依赖于indexPath的UI元素,否则最好在单元类而不是cellForItemAtIndexPath中添加这些视图。