将数据从cellforRowatIndexPath传递到UITableViewCell

时间:2016-08-22 05:11:20

标签: ios swift uitableview

我正在尝试传递String"随机文本"从CellForRowAtIndexPath等于位于自定义Cell UserCell内部的属性imageURL。在单元格类的didSet中,我可以打印"随机文本"很好,但是,由于与这个问题无关的原因,我需要打印"随机文本"在懒惰的变种里面。当我尝试在lazy var中打印它,甚至是awakeFromNib时,它给我零。我知道为什么会这样。我假设编译器在初始化任何自身属性之前为自定义单元类运行某些代码。我想知道是否有办法解决这个问题。

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        
    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! UserCell
    cell.imageURL = "random text"
    return cell
}

自定义单元格内部(UserCell)

class UserCell: UITableViewCell {
  var imageURL: String? {  
    didSet{
      print(imageURL)  //prints the imageURL
    }
}

 lazy var profileImageView: UIImageView = {

    let imageView = UIImageView()
    print(imageURL)   /////prints nil 
    return imageView
}()

0 个答案:

没有答案