如何获取所选collectionviewcell的名称?

use*_*265 5 objective-c gesture-recognition selection ios uicollectionviewcell

这是一个简单的问题,我认为这个问题很容易找到,但却没有.我想在collectionview中选择一个单元格.主要问题是我无法将手势识别器附加到原型单元.我想从触摸的单元格上的标签中抓取文本.我在视图中使用了不同功能的名称.

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

T. *_*sen 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...
}
Run Code Online (Sandbox Code Playgroud)

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

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

func manipulateCellAtIndexPath(indexPath: NSIndexPath) {
    if let cell = collectionView?.cellForItemAtIndexPath(indexPath) {
        // manipulate cell
    }
}
Run Code Online (Sandbox Code Playgroud)