![](/img/trans.png)
[英]How to add different images for different cell to a tableView programmatically (Swift 3)
[英]How to add different images for different cell to a tableView programmatically (Swift)
我想將不同的圖像添加到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] = ["Art_gallery", "Amusement_park", "Zoo", "Bar", "Night_club", "Movie_theater", "Restaurant", "Shopping_mall", "Atm", "Gym", "Store", "Spa", "Museum", "Stadium", "Hardware_store", "Casino", "Library", "Painter", "Book_store", "Bowling_alley", "Cafe", "Clothing_store", ]
return list
}
對於列表中的每個項目,我想添加單元格大小的特定圖像,但是我該怎么辦?
編輯
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
return cell
}
List ist靜態的,對嗎?
為什么不將圖像網址(或您需要的內容)添加到對象。 那會解決你的問題^^。 因此,您可以在單元格中將其稱為:)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_identifier")
let category = NearbyPlaces.getCategory()[indexPath.row]
cell.image = category.image
cell.name = category.name
return cell!
}
您無法在評論中閱讀它^^
正如@TMX所說,您可以使用:
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
參見: https : //developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow
和: https : //stackoverflow.com/a/8079765/7510062
而且,如果您剛剛開始編寫代碼,則應遵循以下教程: https : //developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
..為了了解它是如何工作的,那應該很容易!
如果可以創建自定義單元,那就更好了。 而不是使用相同的代碼,只在CellForRowAtIndex方法的行下面。
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 = list[indexPath.row].isSelected ? .checkmark : .none
cell.textLabel?.text = list[indexPath.row].name
cell.imageView?.image = list[indexPath.row].image // make sure you are saving images in struct.
return cell
}
初始化為
init(name:String, image:UIImage) {
self.name = name
self.image = image
}
將func更改為
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)]
return list
}
擴展代碼:
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.