繁体   English   中英

如何快速在委托sizeForItemAtIndexPath中创建collectionview单元格的对象

[英]how can i create object of collectionview Cell in delegate sizeForItemAtIndexPath in swift

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

将该方法转换为快速方法,如下所示

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM