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:
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)
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?
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:
func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
See: https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow
And: 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
..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.
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
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())
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.