在我的集合视图中,允许多次选择。
当我点按某个项目时,系统会自动选择一个不可见的项目。
这是我的代码:
-(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];
}
有什么建议吗?
答案 0 :(得分:-1)
这是因为你声明你的细胞的2倍:
ButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCell"
forIndexPath:indexPath];
和
ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath];
尝试在.h文件中声明你的单元格并替换我在代码中编写的代码而不使用类ButonCell:
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCell"
forIndexPath:indexPath];
和
cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath];