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

时间:2017-11-07 14:34:21

标签: ios swift xcode uitableview swift3

我正在尝试将不同的图像添加到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] = [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
    }

最后我将它添加到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
    }

我的问题是,在我决定添加图片后,所以在结构中我添加了图片var image:UIImage,我加入QCategoryy的扩展名 在行self.init(name: value)错误:参数标签'(名称:)'与任何可用的重载都不匹配。我该怎么做才能解决这个问题?我的目标是能够将图像添加到tableView的不同单元格中(显然与正确的名称相关联)。

1 个答案:

答案 0 :(得分:0)

我认为您的错误可能是因为您的var isSelected正在初始化,但其他变量并非如此。因此,您可能希望在声明中初始化每个人,如下所示:

struct QCategoryy {
    var name = ""
    var image = UIImage()
    var isSelected = false
}

或者像这样:

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
    }
}

由于您在声明这些变量时未初始化nameimage,因此您必须以某种方式初始化它们。

在您的结构中的init函数中,您希望nameimage作为参数,因此您必须在init调用内部添加它们extension您还需要初始化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())
    }
}