I'm trying 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)
}
}
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] = [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
}
and finally i add this to the 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
}
my problem is that after i decided to add the images, so in the struct i added the image var image:UIImage
i get in the extension of QCategoryy
at the lines self.init(name: value)
the error: Argument labels '(name:)' do not match any available overloads. How can i do to solve this problem? My goal is just to be able to add the images to the different cells of the tableView (associated with the right name obviously).
I think your error is probably because your var isSelected
is being initialized but the other variables aren't. So you might want to initialize everyone at their declaration, like this:
struct QCategoryy {
var name = ""
var image = UIImage()
var isSelected = false
}
or like this:
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
}
}
As you're not initialising neither name
nor image
when you declare those variables, you have to initialise them somehow.
In your init
function in your struct you're expecting name
and image
as parameters, so you have to add them in you init
call inside the extension
and you also need to initialise your image
variable
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.