簡體   English   中英

如何通過NSArray顯示自定義UICollectionViewCell?

[英]How to show custom UICollectionViewCell by NSArray?

我有三個自定義UICollectionViewCell

  • RepoOfTheMonthCell
  • FromTheEditorsCell
  • 世界高級細胞

我有一個NSArray包含這些單元格的順序以及這些單元格應顯示的內容。 (“ cellClass”是一個類。)

(
    "<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. 世界高級細胞

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM