我正在尝试将不同的图像添加到 tableView 中的不同单元格,其中我已经有一个字符串列表,这是我的代码,类别的结构:
struct QCategoryy {
var name:String
var image:UIImage
var isSelected = false
init(name:String, image:UIImage) {
self.name = name
self.image = image
}
}
extension QCategoryy: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.name = value
}
init(unicodeScalarLiteral value: String) {
self.init(name: value)
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(name: value)
}
}
这里是我创建列表的地方(然后我将把它添加到 tableView)
import Foundation
import UIKit
import CoreLocation
import Alamofire
class NearbyPlaces {
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "Art_gallery", image: UIImage(named: "art_button.png")!), QCategoryy(name: "Bar", image: UIImage(named: "bar_button.png")!), QCategoryy(name :"Night_club", image: UIImage(named: "nightclub_button.png")!), QCategoryy(name: "Movie_theater", image: UIImage(named: "cinema_button.png")!), QCategoryy(name: "Restaurant", image: UIImage(named: "restaurant_button.png")!), QCategoryy(name: "Gym", image: UIImage(named: "gym_button.png")!), QCategoryy(name: "Spa", image: UIImage(named: "spa_button.png")!), QCategoryy(name: "Museum", image: UIImage(named: "museum_button.png")!)]
return list
}
最后我将它添加到 tableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "CATEGORY_CELL"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
/* cell.accessoryType = rowIsSelected ? .checkmark : .none */
cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none
cell.textLabel?.text = list[indexPath.row].name
cell.imageView?.image = list[indexPath.row].image
return cell
}
我的问题是,在我决定添加图像之后,所以在结构中我添加了图像 var image:UIImage
我得到了 QCategoryy
的扩展名
在 self.init(name: value)
行出现错误:参数标签 '(name:)' 不匹配任何可用的重载。我该怎么做才能解决这个问题?我的目标只是能够将图像添加到 tableView 的不同单元格(显然与正确的名称相关联)。
最佳答案
我认为你的错误可能是因为你的 var isSelected
正在被初始化,但其他变量没有。所以你可能想在他们的声明中初始化每个人,像这样:
struct QCategoryy {
var name = ""
var image = UIImage()
var isSelected = false
}
或者像这样:
struct QCategoryy {
var name:String
var image:UIImage
var isSelected: Bool
init(name:String, image:UIImage, isSelected: Bool) {
self.name = name
self.image = image
self.isSelected = isSelected
}
}
由于在声明这些变量时既没有初始化name
也没有初始化image
,因此您必须以某种方式初始化它们。
在结构体的 init
函数中,您需要 name
和 image
作为参数,因此您必须将它们添加到 init
在 extension
中调用,你还需要初始化你的 image
变量
extension QCategoryy: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.name = value
self.image = UIImage()
}
init(unicodeScalarLiteral value: String) {
self.init(name: value, image: UIImage())
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(name: value, image: UIImage())
}
}
关于ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160466/