如何通过 NSArray 显示自定义 UICollectionViewCell?

How to show custom UICollectionViewCell by NSArray?

我有3个自定义UICollectionViewCell.

我有一个 NSArray 包含这些单元格的顺序和这些单元格应显示的内容。 ('cellClass' 是一个 class。)

(
    "<RSHomeItem> \n   [title]: REPO\n   OF THE\n   MONTH\n   [img]: https://nexusrepo.kro.kr/repostore/img0.png\n   [url]: https://nexusrepo.kro.kr/\n   [cellClass]: RepoOfTheMonth\n   [subtitle]: <nil>\n</RSHomeItem>",
    "<RSHomeItem> \n   [title]: REPO STORE\n   [img]: https://nexusrepo.kro.kr/repostore/img1.png\n   [url]: <nil>\n   [cellClass]: FromTheEditors\n   [subtitle]: Welcome to the\n   Repo Store!\n</RSHomeItem>",
    "<RSHomeItem> \n   [title]: FEATURED REPO\n   [img]: https://nexusrepo.kro.kr/repostore/img2.png\n   [url]: https://nexusrepo.kro.kr\n   [cellClass]: WorldPremiere\n   [subtitle]: <nil>\n</RSHomeItem>"
)

所以,在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 我像这样循环 NSArray

self.listHome 是一个 NSArray。这个数组很特殊,所以我需要使用 RSHomeItem(JSONModel) 来获取字符串中 'cellClass' 的值。

for (RSHomeItem *item in self.listHome) {
    NSLog(@"class: %@", item.cellClass);
}

此方法已记录

class: RepoOfTheMonth
class: FromTheEditors
class: WorldPremiere

所以我写了这个

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    int i = 0;
    for (RSHomeItem *item in self.listHome) {
        NSIndexPath *iIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        if ([item.cellClass isEqualToString:@"RepoOfTheMonth"]) {
            RepoOfTheMonthCell *repo = [collectionView dequeueReusableCellWithReuseIdentifier:@"RepoOfTheMonth" forIndexPath:iIndexPath];
            [repo.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
            [repo withTitle:item.title URL:item.url];

            return repo;
        } else if ([item.cellClass isEqualToString:@"FromTheEditors"]) {
            FromTheEditorsCell *editors = [collectionView dequeueReusableCellWithReuseIdentifier:@"FromTheEditors" forIndexPath:iIndexPath];
            [editors.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
            editors.titleLabel.text = item.title;
            editors.subtitleLabel.text = item.subtitle;

            return editors;
        } else if ([item.cellClass isEqualToString:@"WorldPremiere"]) {
            WorldPremiereCell *premiere = [collectionView dequeueReusableCellWithReuseIdentifier:@"WorldPremiere" forIndexPath:indexPath];
            [premiere.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
            [premiere withTitle:item.title URL:item.url];

            return premiere;
        }

        i = i + 1;
    }

    return nil;
}

构建良好,但它显示三个 RepoOfTheMonthCell 具有相同的数据,而不是按此顺序显示单元格。

  1. RepoOfTheMonthCell
  2. FromTheEditorsCell
  3. WorldPremiereCell

numberOfSection 为“1”,numberOfRow 为“self.listHome.count”

好的,我不需要循环它。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RSHomeItem *item = self.listHome[indexPath.row];

    if ([item.cellClass isEqualToString:@"RepoOfTheMonth"]) {
        RepoOfTheMonthCell *repo = [collectionView dequeueReusableCellWithReuseIdentifier:@"RepoOfTheMonth" forIndexPath:indexPath];
        [repo.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
        [repo withTitle:item.title URL:item.url];

        return repo;
    }

    if ([item.cellClass isEqualToString:@"FromTheEditors"]) {
        FromTheEditorsCell *editors = [collectionView dequeueReusableCellWithReuseIdentifier:@"FromTheEditors" forIndexPath:indexPath];
        [editors.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
        editors.titleLabel.text = item.title;
        editors.subtitleLabel.text = item.subtitle;

        return editors;
    }

    if ([item.cellClass isEqualToString:@"WorldPremiere"]) {
        WorldPremiereCell *premiere = [collectionView dequeueReusableCellWithReuseIdentifier:@"WorldPremiere" forIndexPath:indexPath];
        [premiere.imageView sd_setImageWithURL:[NSURL URLWithString:item.img]];
        [premiere withTitle:item.title URL:item.url];

        return premiere;
    }

    return nil;
}