2013-04-05 19 views
-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]; 
} 

有什么建议?

+1

这是因为小区重用。如果你不理解这个概念,那么你需要阅读UITableView和UICollectionView的文档,它们都重用了它们的单元格。 – rdelmar 2013-04-05 19:20:16

回答

-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]; 
相关问题