-2
在我的集合视图中允许多选。UICollectionView的问题 - 一个选定单元意味着其他选择
当我点击一个项目时,系统自动选择一个不可见的项目。
这里是我的代码:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCell"
forIndexPath:indexPath];
NSString *title = self.ingredientsBook.names[indexPath.item];
cell.label.text = title;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.searchButton.enabled = YES;
ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.label.textColor = [UIColor blueColor];
NSString *name = self.ingredientsBook.names[indexPath.item];
[self.selectedIngredientNames addObject:name];
}
有什么建议?
这是因为小区重用。如果你不理解这个概念,那么你需要阅读UITableView和UICollectionView的文档,它们都重用了它们的单元格。 – rdelmar 2013-04-05 19:20:16