【问题标题】:organizing dates in uitableview sections在 uitableview 部分中组织日期
【发布时间】:2019-04-08 19:09:07
【问题描述】:

我不知道从哪里开始... 我需要一个TableView 来显示按日期组织的信息。 我创建了一个“模型”类来保存数据...

class Model: NSObject {

var start: Date?
var str: String?

override init()
{

}

init(start: Date, str: String) {

    self.start = start
    self.str = str

}

创建该类的元素

let value1 = Model(start: Date1(), str: string1)
let value2 = Model(start: Date2(), str: string2)

填充该元素的数组:

var array = [Model]()
array.append(value1, value2)

填充TableView 我如何将array 划分为月份或工作周等... 我希望tableView 组织sections 中的数据!?

func numberOfSections(in tableView: UITableView) -> Int {
    return array.count
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    label.text = "January", Febuary, e.g.
    return label
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ?
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
cell.label.text = ?

    return cell
}

非常感谢任何帮助!

【问题讨论】:

标签: ios swift tableview


【解决方案1】:

按功能使用字典组。您可以将数组组织成键和值,键将是您的部分,值将是您的原始数据。

按天分组示例:

extension Date {
    var day: Int? {
        let components = Calendar.current.dateComponents([.day], from: self)
        return components.day
    }
}

let array = [model1, model2]

let dict = Dictionary(grouping: array) { (model) -> Int in
    return model.start?.day ?? 0
}

例如,假设模型 1 上的“开始”参数的日期是星期日 而model2上“start”参数的日期是星期一

所以 dict 会这样分组:

[1: [model1], 2: [model2]]

现在您可以将键用作部分,将值用作行

func numberOfSections(in tableView: UITableView) -> Int {
    return dict.keys ?? 0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let day = dic.keys[section]
    let label = UILabel()
    label.text = String(day)
    return label
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let key = dict[section]
    return dict[key].count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
    let data = dict[indexPath.section]
    cell.label.text = String(data[indexPath.row].start.day)

    return cell
}

【讨论】:

  • 很好的方法——这实际上让我深入了解了字典的功能......学到了很多东西!非常感谢
【解决方案2】:

根据您的情况,生成 2d 数组 将是一个合适的选择。

怎么做?

let transfomredArray = Array(Dictionary(grouping: array, by: { $0. start! }).values)

因此transfomredArray是一个数组数组,每个数组都应该包含具有相同日期的模型。

因此你可以根据它来处理你的tableview数据源方法,例如:

func numberOfSections(in tableView: UITableView) -> Int {
    return transfomredArray.count
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    let date = transfomredArray[section][0].start!
    label.text = "\(date)"
    return label
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TC_Arbeitszeit
    cell.label.text = transfomredArray[indexPath.section][indexPath.row]

    return cell
}

【讨论】:

    【解决方案3】:

    现在你的模式是一个简单的数组。由于您需要分组,因此您的模型需要稍作更改,以便它包含部分/组。您可以从一个数组字典开始,其中的键是月份或工作周。

    查看Swift's reduce:into:,您可以在其中迭代现有数组并将其分解为这样的字典。

    然后将字典的键排序到“sections”数组中。这个计数是您的表格视图的部分计数。所以现在你的模型有一个sections 数组和一个字典dateInfo

    当表格视图请求一行时,在sections数组中查找键let key = sections[indexPath.section],然后找到模型项本身:

    var dateInfo: [Date: [Model]]
    var sections: [Date] // sorted
    ...
    let sectionContent = dateInfo[key] as! [Model]
    // rowCount is just sectionContent.count
    let rowInfo = sectionContent[indexPath.row]
    // populate cell...
    

    希望这足以让您朝着正确的方向前进。

    【讨论】:

    • 非常感谢!帮了大忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多