将 UICollectionView 与自定义 UICollectionViewCell 结合使用,我试图仅向第一个单元格添加一些额外信息。为此,我重写了 GetCell 方法
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
这很好用,第一个单元格显示了额外的信息。问题是单元格在 CollectionView 中再次被重新使用,额外的信息再次显示在那里。
如何防止重复使用 Collection View 中的第一个单元格?
谢谢!
最佳答案
几个选项:
- 覆盖 UICollectionViewCell 的 prepareToReuse 方法,并重新设置 你在那个方法中想要什么
- 首先使用不同的 reuseIdentifiers 和其他细胞
- 对第一个和其他使用不同的原型(prototype)/类 细胞
要使用不同的重用标识符: 如果您通过原型(prototype)在 Storyboard 中执行此操作,请为第一个单元格创建另一个原型(prototype)并在您的 UITableViewDatasource 中执行类似的操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTableCell" forIndexPath:indexPath];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"UsualTableCell" forIndexPath:indexPath];
}
return cell;
}
关于ios - UICollectionView 仅防止第一个单元格重复使用单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27434454/