gpt4 book ai didi

uitableview - 将图像添加到单元格中

转载 作者:IT王子 更新时间:2023-10-29 05:10:09 26 4
gpt4 key购买 nike

我看了很多问题,但似乎找不到答案

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

cell.imageView.image = UIImage(named: "osx_design_view_messages")

这段代码应该可以工作,因为我基本上是从一个问题的答案中复制的,但由于某种原因它没有工作。有什么建议吗?

我用的是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
}

最佳答案

以下是您的问题的一些可能原因:

  1. 单元格原型(prototype)需要包含一个 UIImageView。如果您使用的是 Storyboard,请选择单元格原型(prototype)并确保其样式在属性检查器(右侧面板)中设置为“基本”。

  2. 单元格标识符与 Storyboard中为原型(prototype)单元格键入的标识符不匹配。确保也在属性检查器中为“标识符”输入“单元格”。

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

    println("加载的图片:\(image)")

检查 Storyboard中的所有设置后,尝试使用以下代码运行它:

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
}

关于uitableview - 将图像添加到单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com