[英]How to add different images for different cell to a tableView programmatically (Swift)
I want to add different images to different cell in a tableView where i have already a list of string, this is my code, the struct of category: 我想将不同的图像添加到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)
}
}
and here is where i create the list (which i will then add to the tableView) 这是我创建列表的地方(然后将其添加到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
}
for each item in the list i want to add a specific image of the cell size but how can i do? 对于列表中的每个项目,我想添加单元格大小的特定图像,但是我该怎么办?
EDIT 编辑
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
}
The List ist static, right? List ist静态的,对吗?
Why do you not add an image url (or what u need) to your object. 为什么不将图像网址(或您需要的内容)添加到对象。 That would solve your problem ^^. 那会解决你的问题^^。 So you can call it in cell for row :) 因此,您可以在单元格中将其称为:)
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!
}
You cant read it in the comments^^ 您无法在评论中阅读它^^
As @TMX said, you can use: 正如@TMX所说,您可以使用:
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
See: https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow 参见: https : //developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow
And: https://stackoverflow.com/a/8079765/7510062 和: https : //stackoverflow.com/a/8079765/7510062
And if you just started to code, you should follow this tutorial: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html 而且,如果您刚刚开始编写代码,则应遵循以下教程: https : //developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
..in order to understand how it works, then it should be EASY! ..为了了解它是如何工作的,那应该很容易!
It would be better if you can create custom cells. 如果可以创建自定义单元,那就更好了。 rather with your same code you just below line in CellForRowAtIndex method. 而不是使用相同的代码,只在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
}
initalize as 初始化为
init(name:String, image:UIImage) {
self.name = name
self.image = image
}
change func as 将func更改为
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)]
return list
}
extension code: 扩展代码:
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.