我应该使用哪种方法在UICollectionViewCells之间做一些事情?

时间:2013-12-12 20:02:58

标签: ios objective-c uicollectionview uicollectionviewcell

我已经实施了UICollectionView图片库。每个cell都有一些视图,我想在我更改当前cell时隐藏或显示该视图,至少在事件开始时。有什么方法吗?或者我应该由代表做什么?我有paging enabled和自定义cell以及FlowLayout

我已经做了几乎所有事情tutorial

1 个答案:

答案 0 :(得分:1)

一种方法是将当前选定的单元格索引保留在viewController中的局部变量中,并在选择另一个单元格时使用该索引执行任何操作:

@property (nonatomic) NSIndexPath *selectedCellIndexPath;

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (![self.selectedCellIndexPath isEqual:indexPath]) {
        UICollectionViewCell *lastSelectedCell = [collectionView cellForItemAtIndexPath:selectedIndexPath];
        // Perform any change to lastSelectedCell before deselecting it
        [collectionView deselectItemAtIndexPath:lastSelectedIndexPath animated:YES];
    }
    self.selectedCellIndexPath = indexPath;
    UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];
    // Change what you want in the newly selected cell;
}