2017-03-04 67 views
0

**如何选择的CollectionView单细胞和取消选择所有其它细胞

我已经水平的CollectionView其中选择的单元有橙色 渐变颜色和所有其他取消细胞灰色IM 仅使用didselect委托方法但我正在逐渐 多个单元格的选择问题,我有表 视图与同一个概念的问题,同时滚动和我寻觅了很多,但我还没有得到重用细胞的 正确答案

**

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 


    DateTimeCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DateTimeCellID" forIndexPath:indexPath]; 



    if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath) 
    { 

     CAGradientLayer *gradient = [CAGradientLayer layer]; 
     gradient.frame = cell.mainView.bounds; 
     gradient.startPoint = CGPointZero; 

     gradient.endPoint = CGPointMake(1, 1); 
     gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:238.0/255.0 green:42.0/255.0 blue:123/255.0 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:241.0/255.0 green:90.0/255.0 blue:41.0/255.0 alpha:1.0] CGColor], nil]; 
     [gradient setMasksToBounds:NO]; 
     cell.mainView.backgroundColor = [UIColor clearColor]; 
     [cell.mainView.layer insertSublayer:gradient atIndex:0]; 

     [indexPaths addObject:self.selectedIndexPath]; 

    } 
    else 
    { 
     [cell.mainView setBackgroundColor:[self colorWithHexString:@"383F4A"]]; 
     } 

    return cell; 
} 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 


    indexPaths = [NSMutableArray arrayWithObjects:indexPath, nil]; 

    if (self.selectedIndexPath) 
    { 
     // if we had a previously selected cell 

     if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) 
     { 
      // if it's the same as the one we just tapped on, then we're unselecting it 

      NSLog(@"Selected"); 

     } 
     else 
     { 
      // if it's different, then add that old one to our list of cells to reload, and 
      // save the currently selected indexPath 

      [indexPaths addObject:self.selectedIndexPath]; 
      self.selectedIndexPath = indexPath; 
     } 
    } 
    else 
    { 
     // else, we didn't have previously selected cell, so we only need to save this indexPath for future reference 

     self.selectedIndexPath = indexPath; 

    } 



    dispatch_async(dispatch_get_main_queue(), ^{ 
     [collectionView reloadItemsAtIndexPaths:indexPaths]; 
    }); 


} 
+0

您需要重新加载所有行此。 –

+0

多个单元格现在正在单击选中。 –

+0

在重新加载后管理您所需的选定单元格。 –

回答

1

采取全球selectedIndexPath globaly像下面

NSIndexPath *selectedIndexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0]; 

wrilte下面的代码在委托方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 


DateTimeCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DateTimeCellID" forIndexPath:indexPath]; 

if self.selectedIndexPath == indexPath 
{ 
    // do what you want to do with your selected cell 
} 
else 
{ 
    // do what you want to do with your deselected cell 
} 


return cell; 
} 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

self.selectedIndexPath = indexPath 
[self.collectionView reloadData]; 


} 
+0

我在哪里定义全局变量..?像在做select或cell为行? –

+0

在.h文件中,你有你的collectionview的出口 –

+0

同样的问题兄弟你试过.. ?? –

相关问题