Iphone 将collectionView转换为Swift以配置自定义单元格
我已经用Objective-C开发了一个Collection View应用程序。我现在正试图用Swift重新创建它,但我似乎无法翻译最后两段代码 有人能帮我翻译setupCell函数以配置自定义单元格吗?Iphone 将collectionView转换为Swift以配置自定义单元格,iphone,swift,uicollectionview,nsdictionary,uicollectionviewcell,Iphone,Swift,Uicollectionview,Nsdictionary,Uicollectionviewcell,我已经用Objective-C开发了一个Collection View应用程序。我现在正试图用Swift重新创建它,但我似乎无法翻译最后两段代码 有人能帮我翻译setupCell函数以配置自定义单元格吗? override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let
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"];
}
}
查看此代码:
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
}
让我知道它是否适用于您,因为我没有测试它函数是否有效:)。您知道如何将此单元格setupCell:[self.titles.objectAtIndex(indexath.row)转换为self.setupCell(self.title.objectAtIndex(indexath.row))