就像在Objective中一样,我们可以得到CollectionViewCell的对象,如下所示。与Swift有一些问题:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
DatasetCell *datasetCell = (DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
(DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
datasetCell.backgroundColor = [UIColor skyBlueColor];
}
我的快捷代码是:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let datasetCell: DatasetCell = collectionView.cellForItemAtIndexPath(indexPath) as! DatasetCell
datasetCell.backgroundColor = UIColor.blueColor()
}
答案 0 :(得分:1)
要将该方法转换为swift,它看起来像这样
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let datasetCell = collectionView(collectionView, cellForItemAtIndexPath: indexPath) as DatasetCell
datasetCell.backgroundColor = UIColor.blueColor()
}