【问题标题】:How to add different images for different cell to a tableView programmatically (Swift)如何以编程方式将不同单元格的不同图像添加到 tableView (Swift)
【发布时间】:2017-11-07 10:00:48
【问题描述】:

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

【问题讨论】:

    标签: ios swift uitableview swift3 custom-cell


    【解决方案1】:

    如果您可以创建自定义单元格会更好。而不是在 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
        }
    

    扩展代码:

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

    【讨论】:

    • 我对列表中的每个名字都有不同的图像
    • @Tvenden 为您的答案更新了我的代码。 :)。希望这会奏效
    • 谢谢,我认为这可行,但最后一个问题,我必须在哪里添加图像列表?
    • 初始化时。 init(name:String, image:UIImage) { self.name = name self.image = image }
    • 因为正如您在我的代码中看到的,我在结构中添加了 init(image:UIImage) { self.image = image } 但我收到错误:“声明仅在文件范围内有效”跨度>
    【解决方案2】:

    列表是静态的,对吗?

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

    【讨论】:

    • 是的,列表是静态的,我可以试试,但我不知道该怎么做
    • 只需在您的 QCategory 中添加一个图像或 url 、字符串(无论什么......)。将其添加到您的初始化中。我不确定你的扩展名。你需要它们吗,它们是必需的吗?如果不是:只需将您的静态更改为: let list = [QCategory(name: "Your name" , image : "imageName"), ...] 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 返回单元格!}
    【解决方案3】:
    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!
    
    }
    

    在cmets看不到^^

    【讨论】:

    • 我试过了,如果你现在看到我的 QCategory 结构,我编辑了代码,但我在扩展中收到错误:“声明仅在文件范围内有效”
    • 更新了我的回答。需要在初始化的时候添加。
    【解决方案4】:

    正如@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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 2015-04-27
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多