我有三个自定义UICollectionViewCell
。
,我有一个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
,而不是按此顺序显示单元格。
numberOfSection为“ 1”,numberOfRow为“ self.listHome.count”
答案 0 :(得分:0)
好吧,我不需要循环它。
- (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;
}