【问题标题】:UIImageView not showing image in custom UITableViewCellUIImageView 未在自定义 UITableViewCell 中显示图像
【发布时间】:2014-11-21 11:21:55
【问题描述】:

UIImageView 没有在我的自定义UITableViewCell 中显示图像。

不知道为什么,我尝试了在Interface Builder 中设置Image 属性的两种方法,然后尝试设置IBOutlet 和设置.image 属性。

似乎没有一个工作。为什么会发生在我身上?!?!

带有 TableViewDataSource 的类

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("LatestMessageCell") as LatestMessageCell

    cell.avatarImageView.image = UIImage(named: "Logo")

    return cell
}

自定义单元类

class LatestMessageCell: UITableViewCell {

@IBOutlet weak var cardBackgroundView: UIView!

@IBOutlet weak var avatarImageView: UIImageView!

@IBOutlet weak var usernameLabel: UILabel!

@IBOutlet weak var messageLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    cardBackgroundView.layer.cornerRadius = 4
    cardBackgroundView.layer.shadowColor = UIColor.blackColor().CGColor
    cardBackgroundView.layer.shadowOffset = CGSize(width: 0, height: 2)
    cardBackgroundView.layer.shadowOpacity = 0.3
    cardBackgroundView.layer.shadowRadius = 5
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

【问题讨论】:

  • 我会说: • 检查插座是否设置正确 • 检查图像是否已加载(可能是错误的图像名称?)

标签: ios uitableview swift uiimageview


【解决方案1】:

请修改您的方法并检查:

let simpleTableIdentifier = "TableViewCell";

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

      var cell:LatestMessageCell? = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? LatestMessageCell

      if (cell == nil)
      {
           let nib:Array = NSBundle.mainBundle().loadNibNamed("LatestMessageCell", owner: self, options: nil)
           cell = nib[0] as? LatestMessageCell
      }

      cell!.avatarImageView.image = UIImage(named: "Logo")

      return cell!
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多