【问题标题】:Best way to populate rows in section in UITableView?在 UITableView 部分中填充行的最佳方法?
【发布时间】:2017-01-01 19:20:00
【问题描述】:

我正在构建一个应用程序,其中我在 UITableView 中有几个部分。我目前的解决方案是在字典中收集我的数据,然后为每个部分选择一个键。有没有更好的解决方案?

【问题讨论】:

  • 1) 节数 = 字典中的键数 2) 节中的行数 = 索引(节)处字典中的值数
  • 没错,这就是这样做的方法?
  • 是的,这是正确的方法。我也要分享代码吗?
  • 会很好,谢谢。
  • 使用模型类。部分的模型类称为 SectionModel,在其中创建一个数组,该数组将保存另一组模型对象,称为 RowModel。所以通常一个数组会保存一组 SectionModel 对象,每个 SectionModel 对象都会保存一个 RowModel 对象数组。

标签: ios swift uitableview dictionary


【解决方案1】:

其中一种好方法 - 直接模型映射,尤其适用于快速枚举。 例如,您有 2 个不同的部分和 3 种不同类型的行。您的枚举和 ViewController 代码将如下所示:

enum TableViewSectionTypes {
    case SectionOne
    case SectionTwo
}

enum TableViewRowTypes {
    case RawTypeOne
    case RawTypeTwo
    case RawTypeThreeWithAssociatedModel(ModelForRowTypeNumberThree)
}

struct ModelForRowTypeNumberThree {
    let paramOne: String
    let paramTwo: UIImage
    let paramThree: String
    let paramFour: NSData
}

struct TableViewSection {
    let type: TableViewSectionTypes
    let raws: [TableViewRowTypes]
}

class ViewController: UIViewController, UITableViewDataSource {

    var sections = [TableViewSection]()

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].raws.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let raw = sections[indexPath.section].raws[indexPath.row]
        switch raw {
        case .RawTypeOne:
            // Here return cell of first type
        case .RawTypeTwo:
            // There return cell of second type
        case .RawTypeThreeWithAssociatedModel(let modelForRawTypeThree):
            // And finally here you can use your model and bind it to your cell and return it
        }
    }
}

有什么好处?对各种细胞类型的强类型化、显式建模和显式处理。在这种情况下,您必须做的唯一简单的事情就是将您的数据解析到这个枚举和结构中,以及为您的字典执行此操作。

【讨论】:

  • 这似乎很有希望解决我的问题。但是我不知道如何将我的firebase数据库中的数据解析到枚举中。我可以将数据库中的数据直接加载到枚举中吗?或者我在哪里执行这个步骤?我已经有一个像 ModelForRowTypeNumberThree 这样的结构。在我的应用程序中,所有行类型都是相同的,但由 4 个不同的 NSDictionarys 填充。这可能吗?
【解决方案2】:

这是我写的一个简单的例子。请注意,它很容易出错,因为它没有检查密钥是否存在,也没有创建正确的单元格。

您也可以使用字典来执行此操作,因为您可以迭代其内容。

希望对你有帮助:

class AwesomeTable: UITableViewController {

    private var tableContent: [[String]] = [["Section 1, row 1", "Section 1, row 2"], ["Section 2, row 1", "Section 2, row 2"]]

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return tableContent.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableContent[section].count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        let item = tableContent[indexPath.section][indexPath.row]

        cell.textLabel?.text = item

        return cell
    }

}

【讨论】:

  • 这不是一个很好的做法。 1)。大规模地维护该词典的内容非常困难。 2)。对大字典进行排序或排序几乎是不可能的。 3)。创建非常复杂的单元格非常困难 4)。如果您有超过 1 或 2 种细胞类型,它会在您的 cellForRowAtIndexPath 中变得混乱 5)。没有明确设置。你只转发正确的字典内容,这是不好的。
  • 我同意,但是考虑到它回答的问题。有很多方法可以更好地做到这一点,其中一种就是你给出的答案。在您的回答中,代码更清晰,更易于阅读,但是我想争辩说复杂性仍然大致相同。
【解决方案3】:

如下实现table view datasource:-

1) 设置number of sections = 字典中的键数

2) No of rows in section = no of values in dictionary at index(section)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    相关资源
    最近更新 更多