将collectionView转换为Swift以配置自定义单元格

时间:2014-11-19 07:51:01

标签: iphone swift uicollectionview nsdictionary uicollectionviewcell

我使用Objective-C开发了一个Collection View应用程序。我现在正尝试使用Swift重新创建它,但我似乎无法翻译最后两段代码。

有人可以帮我翻译我的setupCell功能以配置自定义单元格吗?

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell

    ###Translate to Swift
    cell setupCell:[self.titles.objectAtIndex(indexPath.row)

    return cell
}

class CollectionViewCell: UICollectionViewCell {

  ###Translate to Swift
  -(void)setupCell:(NSDictionary *)dictionary {
    NSString *imageFileName = [dictionary valueForKey:@"image"];
    self.imageView.image = [UIImage imageNamed:imageFileName];

    self.nameLabel.text = [dictionary valueForKey:@"name"];

  }
}

1 个答案:

答案 0 :(得分:2)

查看此代码:

 func setUpCell(dictonary:NSDictionary){

    var imageFileName : NSString = dictonary.valueForKey("image") as NSString
    self.imageView.image = UIImage(named:imageFileName)
    self.nameLabel.text = dictonary.valueForKey("name") as NSString

}

让我知道它是否适合你,因为我没有测试它