繁体   English   中英

如何以编程方式将不同单元格的不同图像添加到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
    }

List ist静态的,对吗?

为什么不将图像网址(或您需要的内容)添加到对象。 那会解决你的问题^^。 因此,您可以在单元格中将其称为:)

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 : //stackoverflow.com/a/8079765/7510062

而且,如果您刚刚开始编写代码,则应遵循以下教程: 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
        }

将func更改为

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

扩展代码:

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