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

How to add different images for different cell to a tableView programmatically (Swift)

我想在 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] = ["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
    }

我想为列表中的每个项目添加单元格大小的特定图像,但我该怎么做?

编辑

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
    }

列表是静态的,对吧?

为什么不向对象添加图像 url(或您需要的图像)。那会解决你的问题^^。所以你可以在行的单元格中调用它:)

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!

}

评论区看不懂^^

正如@TMX 所说,您可以使用:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?

参见:https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow

并且:

如果您刚开始编写代码,则应遵循本教程:https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html

..为了理解它是如何工作的,那么它应该很容易!

如果能创建自定义单元格就更好了。而不是使用您在 CellForRowAtIndex 方法中的行下方的相同代码。

    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
        }

初始化为

init(name:String, image:UIImage) {
            self.name = name
            self.image = image
        }

将函数更改为

static func getCategories() -> [QCategoryy] {
        let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)]
        return list
    }

分机号:[=​​14=]

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