ios - UICollectionView 重新加载数据但不显示新单元格

标签 ios objective-c ios7 uicollectionview

我有一个由数组 titlesMutable 填充的 UICollection View ,当 View 首次加载时单元格成功填充(所有 30 个单元格)当我添加新数据时做

[self.collectionView reloadData];

我可以在部分中的项目数中记录 titlesMutable,计数现在是 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];

});

最佳答案

您正在创建两个 UICollectionView,因此当您调用 [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];

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

关于ios - UICollectionView 重新加载数据但不显示新单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828841/

相关文章:

ios - 如何从 UICollectionView 中删除行?

iphone - 如何动态填充 NSArray?

ios - UIPickerView 在 UIView 中不显示添加的 subview

ios - 在动画 block UIStackView 中隐藏排列 subview

ios - 如何使用 PolyLine 在 Swift iOS 中的两个坐标之间绘制路径

iphone - 应用程序因数据模型更改而崩溃

ios - 如何在 iOS 7 上嵌入 youtube 视频

crash - 应用程序在iOS 7上崩溃,但在iOS 6上运行良好

ios - Enterprise build 不是通过服务器安装,而是可以通过 dropbox 安装

iphone - 多采样渲染到 ios 中的纹理