【问题标题】:How to make a dynamic section header from json response如何从 json 响应中制作动态部分标题
【发布时间】:2018-07-21 15:05:55
【问题描述】:

我能够使用 alamofire 对来自 sample json 的 json 响应进行排序

self.responseValue.sorted(by: {(dateFormatter.date(from: $0.object(forKey: "date") as! String))?.compare(dateFormatter.date(from: $1.object(forKey: "date") as! String)!) == .orderedDescending})

我不确定这是否是正确的方法,但这会按日期对整个响应进行排序。我正在尝试按日期(从响应中可用的最新日期到最后一个日期)将其显示到表格视图和名称部分标题。我还不能做到这一点。老实说,我不确定如何实现该标题日期。你能提供一些建议吗?谢谢。

【问题讨论】:

  • 听起来你想要的是这样的stackoverflow.com/questions/26596090/…,但我不确定。你为什么不把你的响应解析成一个对象,并使用这个对象的一个​​数组作为你的 TableView 的数据源
  • 这是一个很好的例子,但是我正在尝试使用“日期”键对 NSDictionary 数组进行排序。如果可能的话,我想用“日期”键作为标题部分和正文对 cellforrowat 函数进行排序
  • 据我了解,这正是链接的 SO-Post 中显示的内容
  • 如何在 cellforrowat 函数中实现它?对不起,我真的有点困惑

标签: ios json swift uitableview alamofire


【解决方案1】:

是的,您可以使用下面的UITableView 数据源方法来提供节标题

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

// Create your view with label here
// access data from your parsed array using the section index
// set the values 
// return the view

}

这是动态的。

【讨论】:

    【解决方案2】:

    感谢 Teetz,这解决了问题

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! sampleTableViewCell
    
        cell.sample.text = "sample"
        cell.sample.delegate = self
    
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多