将图像添加到单元格中

Joh*_*110 23 uitableview swift xcode6

我已阅读大量问题,但似乎无法找到答案

我有一个TableView但无法将图像添加到我的单元格

cell.imageView.image = UIImage(named: "osx_design_view_messages")
Run Code Online (Sandbox Code Playgroud)

这段代码应该有效,因为我基本上将它从一个问题的答案中复制出来但由于某种原因它无法正常工作.有什么建议?

我顺便使用swift

 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    let cellIdentifier = "cell"

    var cell : UITableViewCell

    cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
    cell.textLabel.text = self.recipies[indexPath.row]
    var image : UIImage = UIImage(named: "osx_design_view_messages")
    return cell
}
Run Code Online (Sandbox Code Playgroud)

Das*_*ash 37

以下是您遇到问题的几个可能原因:

  1. 单元原型需要包含UIImageView.如果您正在使用故事板,请选择单元格原型并确保其属性在属性检查器(右侧面板)中设置为"基本".

  2. 单元格标识符与故事板中原型单元格中键入的标识符不匹配.确保在属性检查器中为"标识符"输入"单元格".

  3. 图像未正确加载文件.要对此进行测试,请在初始化图像后添加以下行:

    println("The loaded image: \(image)")

检查故事板中是否已设置所有内容后,请尝试使用以下代码运行它:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    {
        let cellIdentifier = "cell"

        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

        cell.textLabel.text = self.recipes[indexPath.row]

        var image : UIImage = UIImage(named: "osx_design_view_messages")
        println("The loaded image: \(image)")
        cell.imageView.image = image

        return cell
    }
Run Code Online (Sandbox Code Playgroud)

  • 啊谢谢:),建议1是我的答案的解决方案,Swift将需要一些时间来使用 (2认同)