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

标签 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) 行出现错误:参数标签 '(name:)' 不匹配任何可用的重载。我该怎么做才能解决这个问题?我的目标只是能够将图像添加到 tableView 的不同单元格(显然与正确的名称相关联)。

最佳答案

我认为你的错误可能是因为你的 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
    }
}

由于在声明这些变量时既没有初始化name 也没有初始化image,因此您必须以某种方式初始化它们。

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

关于ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160466/

相关文章:

html - 基于开关,在代码运行时将 html 添加到 ui web View

ios - 将自己作为委托(delegate)传递时的无限循环

ios - 使用 pod install 后出现 Swift 错误(clang : error: linker command failed with exit code 1 )

ios - Cocoapods 警告 - CocoaPods 没有设置项目的基本配置,因为你的项目已经有一个自定义配置集

xcode - 重命名时文件名后的问号 - 如何从 Xcode 中清除?

ios - [NSCFArray 长度] : unrecognized selector sent to instance

javascript - 仅针对单个 href 更改检测 iPad(或 iOS)

ios - 设备 UDID 已经存在于我的开发者帐户中,在 firebase 和 faSTLane 中等待开发者时仍然出现错误

ios - 在视频模式下为AVCaptureDevice启用True Tone手电筒

ios - 点击手势在 collectionView 单元格中不起作用,任何 UI 元素在由 Storyboard 添加的单元格上都不可见