简体   繁体   English

如何以编程方式将不同单元格的不同图像添加到tableView(Swift 3)

[英]How to add different images for different cell to a tableView programmatically (Swift 3)

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: 我试图将不同的图像添加到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)
    }
}

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] = [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 最后我将其添加到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. 我的问题是我决定添加图像之后,因此在结构中添加了图像var image:UIImage我在QCategoryy的扩展名self.init(name: value)行中self.init(name: value)了错误:参数标签'(name :)'与任何可用的重载都不匹配。 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). 我的目标只是能够将图像添加到tableView的不同单元中(显然与正确的名称相关联)。

I think your error is probably because your var isSelected is being initialized but the other variables aren't. 我认为您的错误可能是因为您的var isSelected正在初始化,但其他变量没有初始化。 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. 由于在声明这些变量时没有初始化nameimage ,因此必须以某种方式对其进行初始化。

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 在结构的init函数中,您希望使用nameimage作为参数,因此必须在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM