![](/img/trans.png)
[英]How to add different images for different cell to a tableView programmatically (Swift)
[英]How to add different images for different cell to a tableView programmatically (Swift 3)
我試圖將不同的圖像添加到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)
行中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
作為參數,因此必須在extension
init
調用中添加它們,還需要初始化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())
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.