5

这是一个简单的问题,我认为答案很容易找到,但没有。我想在集合视图中选择一个单元格。主要问题是我无法将手势识别器附加到原型单元。我想从被触摸的单元格上的标签中获取文本。在我看来,我在不同的函数中使用了这个名称。

或者一个更简单的问题:是否有关于从项目列表中点击选择的教程?

4

1 回答 1

6

collectionView:didSelectItemAtIndexPath:在委托中有方法。当您收集单元格并为您提供该特定单元格的正确 indexPath 时,这应该会触发。

将此 indexPath 与 collectionView 结合使用cellForItemAtIndexPath:以访问特定单元格。

例子:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self manipulateCellAtIndexPath:indexPath];
}

-(void) manipulateCellAtIndexPath:(NSIndexPath*)indexPath {
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    // Now do what you want...
}

而且,只要我在这里。斯威夫特版本:

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    manipulateCellAtIndexPath(indexPath)
}

func manipulateCellAtIndexPath(indexPath: NSIndexPath) {
    if let cell = collectionView?.cellForItemAtIndexPath(indexPath) {
        // manipulate cell
    }
}
于 2013-02-02T09:45:45.453 回答