【问题标题】:Translate collectionView to Swift to Configure the Custom Cell将 collectionView 转换为 Swift 以配置自定义单元格
【发布时间】:2014-11-19 07:51:00
【问题描述】:

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

有人可以帮我翻译 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"];

  }
}

【问题讨论】:

    标签: iphone swift uicollectionview nsdictionary uicollectionviewcell


    【解决方案1】:

    查看此代码:

     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(indexPath.row)
    • 试试这个:self.setUpCell(self.title.objectAtIndex(indexPath.row))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多