【问题标题】:UICollectionView reloads data but doesn't display new cellsUICollectionView 重新加载数据但不显示新单元格
【发布时间】:2014-11-07 20:18:17
【问题描述】:

我有一个 UICollection 视图,它由数组 titlesMutable 填充,当视图首次加载单元格时,当我添加新数据时,单元格填充成功(全部 30 个)然后做

[self.collectionView reloadData];

我可以将titlesMutable记录在section中的item数中,现在计数是60。所以应该成功了吧?重新加载后,我仍然可以使用 collectionView 之后的正常使用方式。下面是我的代码。我错过了什么还是错过了什么?

设置 CollectionView

    -(void)displayCollection {
    UIColor *myColor = [UIColor colorWithRed:(245.0 / 255.0) green:(245.0 / 255.0) blue:(245.0 / 255.0) alpha: 1];

    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CGRect locOfScree = CGRectMake(0, 44, screenWidth, screenHeight - 44);

    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];

    layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    layout.headerHeight = 0;
    layout.footerHeight = 0;
    layout.minimumColumnSpacing = 2;
    layout.minimumInteritemSpacing = 2;
    self.collectionView = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;

    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    [self.collectionView setBackgroundColor:myColor];

    [self.view insertSubview:self.collectionView belowSubview:navBar];
    //[self.view addSubview:self.collectionView];

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[CHTCollectionViewWaterfallCell class]
        forCellWithReuseIdentifier:CellIdentifier];
}

CollectionView 方法

   - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //return [self.imageURLStrings count];
    NSLog(@"Number of items in section:%lu", (unsigned long)[titlesMutable count]);
    return [titlesMutable count];
    //return 2;

}



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

    //    CHTCollectionViewWaterfallCell *cell =
    //    (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
    //                                                                          forIndexPath:indexPath];
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    NSString *title = [titlesMutable objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    NSString *imageUrl = [thumbMediaUrl objectForKey:title];
    UIImageView *imageView = [[UIImageView alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        //[cell.imageView setImage:[UIImage imageWithData:data]];
        [imageView setImage:[UIImage imageWithData:data]];
        cell.backgroundView = imageView;

    }];

    //cell.backgroundView = imageView;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;

    NSString *title = [titlesMutable objectAtIndex:indexPath.row];
    float width = [[thumbWidth objectForKey:title] floatValue];
    float height = [[thumbHeight objectForKey:title] floatValue];
    float imageWidth = (screenWidth / 2) - 3;
    float scale = imageWidth / width;
    float imageHeight = height * scale;
    CGSize imageSize = CGSizeMake(imageWidth, imageHeight);


    return imageSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* attributesToReturn = [self layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
    {
        if (nil == attributes.representedElementKind)
        {
            NSIndexPath* indexPath = attributes.indexPath;
            attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
        }
    }
    return attributesToReturn;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewLayoutAttributes* currentItemAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];

    if (indexPath.item < numColumns){
        CGRect f = currentItemAttributes.frame;
        f.origin.y = 0;
        currentItemAttributes.frame = f;
        return currentItemAttributes;
    }
    NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
    CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
    CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
    CGRect f = currentItemAttributes.frame;
    f.origin.y = YPointNew;
    currentItemAttributes.frame = f;
    return currentItemAttributes;
}

添加更多数据并重新加载

-(void)addMore {
 titlesMutable = [[NSMutableArray alloc] initWithArray:titles];

dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];



    NSLog(@"Im in the block");
    [self doneLoading];

});

}

【问题讨论】:

  • 你在哪里添加新数据?
  • @dopcn 我在addMore方法上面添加了新的集合叫做titles,所以我用titles填充titlesMutable

标签: ios objective-c ios7 uicollectionview


【解决方案1】:

您正在创建两个UICollectionViews,因此当您调用[self.collectionview reloadData] 时,只会调用第二个collectionview。

第一次收藏

self.collectionView = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

self.collectionView.delegate=self;
self.collectionView.dataSource=self;

[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];

UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
[self.collectionView setBackgroundColor:myColor];

[self.view insertSubview:self.collectionView belowSubview:navBar];
//[self.view addSubview:self.collectionView];

第二次收藏

self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CHTCollectionViewWaterfallCell class]
    forCellWithReuseIdentifier:CellIdentifier];

您似乎希望第一个集合重新加载数据,而不是第二个。

【讨论】:

  • 哇,我不敢相信我错过了,我忘了按计划删除那个部分。非常感谢你,当它让我奖励你时,你会得到你的赏金。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 2014-10-31
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多