如何在swift中的委托sizeForItemAtIndexPath中创建collectionview Cell的对象

时间:2015-12-05 11:58:35

标签: ios swift uicollectionview

就像在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()
}

1 个答案:

答案 0 :(得分:1)

要将该方法转换为swift,它看起来像这样

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let datasetCell =  collectionView(collectionView, cellForItemAtIndexPath: indexPath) as DatasetCell
    datasetCell.backgroundColor = UIColor.blueColor()

}