如何借助表视图中的数组在uiimageview中设置图像


var imgg = ["wall-wallpaper-500x500.jpg", "wall-wallpaper-500x500.jpg", "wall-wallpaper-500x500.jpg"] ... .. ...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let userCell = tableView.dequeueReusableCell(withIdentifier: "ViewTableViewCell", for: indexPath) as? ViewTableViewCell else {
fatalError()
}
if indexPath.section == 0{
userCell.thirdlbl.text = names[indexPath.row]
userCell.firstlbl.text = android[indexPath.row]
userCell.secondlbl.text = "ios"
userCell.buttonLabel.text = "Hello"
userCell.img.image = imgg[indexPath.row] // Cannot assign value of type 'String' to type 'UIImage?'
}
userCell.img.image = UIImage(named: imgg[indexPath.row])

问题是不能在需要UIImage的地方使用String。你需要更新你的代码如下:

userCell.img.image = UIImage(named: imgg[indexPath.row])

[更新]

如果在assets文件夹中使用图像,这些图像通常没有扩展名,因此您可以通过文件名(没有扩展名(引用它们。在这种情况下,您的代码应该是

userCell.img.image = UIImage(named:“wall-wallpaper-500x500”)

最新更新