在带有 swift 的 Xcode 7 中,我尝试使用 Assets 文件夹中的图标显示在普通原型(prototype) tableview 单元格的图标槽中。
我的代码编译成功,应用程序运行良好,在表格中显示所有正确的行文本标题,但所有单元格都使用我(可能是愚蠢地)输入到原型(prototype)表格单元格的属性检查器中的图标。
默认情况下,style=Basic 表格单元格在 Storyboard 中的标题左侧没有图标。但是...如果您在 image=MyAssetsIconName 中添加 Assets 文件夹中的图标图像的名称,UI 构建器会自动将 UIImageView 添加到表格行单元格的左端。当然,所有行都显示您选择的图像。这就是我的应用目前所做的。
但我想更改每一行的图标图像,以便任何行上的图像与我显示的文本行相匹配。因此,我尝试在创建时将新图像(来自 Assets 文件夹)分配给每个行单元格。我在将文本标题分配给单元格后立即进行了分配。
这是我的代码,运行良好(但不显示我想要的图像)。
class TsbReportsCell: UITableViewCell {
@IBOutlet weak var Title: UILabel!
@IBOutlet weak var Detail: UILabel!
@IBOutlet weak var Icon: UIImageView!
...
}
在 TableView Controller 中:
In the table view controller class:
// define datasources
var reportnames = [String]()
var reportimages = [String]()
override func viewDidLoad() {
super.viewDidLoad()
reportnames = ["Balance Report",
"Routine Report",
"Low Quality Report"]
reportimages = ["enter.png",
"exterior.png",
"export.png"]
}
override func tableView(tableView:.... {
// all this code works fine
let cell = tableView.dequeueReusableCellWithIdentifier...
let row = indexPath.row
cell.Title.text = reportnames[row]
// here are the lines that seem to have no effect at all
let rowimage = UIImage(named: reportimages[row])
cell.Icon = UIImageView(image: rowimage)
return cell
}
知道我做错了什么吗?我在表格单元格行的属性中命名了一个图像,只是为了让 UI 构建器为我添加 imageview 占位符。但也许我永远不会被允许覆盖该图像,即使我可以为它创建一个导出。
这个问题看起来很简单,但我搜索了至少 20 个帖子、网络、文档,但我仍然找不到任何东西。谢谢
最佳答案
非常简单。你是说:
cell.Icon = UIImageView(image: rowimage)
但这并没有将这个 ImageView 放入界面!它只是将其分配给单元格的属性。
相反,您需要在单元格中 有一个 ImageView ,并将单元格的 Icon
属性的 socket 连接到该 ImageView 。因此,cell.Icon
是界面中的 ImageView 。现在你说
cell.Icon.image = rowimage
关于 Assets 文件夹中原型(prototype) TableView 单元格的 xcode 7.3 引用图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244048/