【问题标题】:How to sort JSON data alphabetically in tableview cell如何在表格视图单元格中按字母顺序对 JSON 数据进行排序
【发布时间】:2019-12-10 11:35:30
【问题描述】:

我从服务器获取 JSON 格式的用户数据。现在我可以获取用户详细信息,但它会自动按日期排序。以下是从服务器获取用户详细信息的代码:

func getAllDoctor(){

    let param = ["page":"1"]
    print(param,"param123")
    Alamofire.request(helper.MainURL + "patient/getAllDoctor", method:.post,parameters: param).responseJSON { response in

        self.stopAnimating()

        if let result = response.result.value {
            print(1)
            let DictResponse = JSON(result as! NSDictionary)

            if DictResponse["success"].boolValue
            {
                self.marrDoctorList = DictResponse["data"].arrayValue                    
                self.tblDoctors.reloadData()
            }

        }

        }.responseString { (strREsopns) in
            print(strREsopns)
    }

}

这里是tableview函数:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return  UITableView.automaticDimension
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllDoctorsTVC

    cell.selectionStyle = .none
    cell.viewBG.layer.cornerRadius = 5
    cell.viewBG.layer.borderWidth = 1
    cell.viewBG.layer.borderColor = UIColor.lightGray.cgColor

    cell.lblName.text = self.marrDoctorList[indexPath.row]["name"].stringValue
    cell.lbl1.text = self.marrDoctorList[indexPath.row]["categories_name"].stringValue

    cell.imgDoctor.sd_setImage(with: URL(string: marrDoctorList[indexPath.row]["profile_image"].stringValue), placeholderImage: UIImage(named: ""))
    cell.imgDoctor.layer.cornerRadius = cell.imgDoctor.frame.height / 2
    cell.imgDoctor.clipsToBounds = true

    cell.imgRead.isHidden = true

    return cell
}

我想按用户名的字母顺序对它们进行排序。

tableView(作为 JSON 对象)重新加载时的数据结构本质上是:

{"success":true,"data":[
{"doctor_id":"149","name":"Ferit Dadasli","description":"Ortodontist","expertise_categories":"2","categories_name":"Di\u015f Hekimi","profile_image":"http:\/\/www.....net\/app\/assets\/default\/user1.png"},
{"doctor_id":"141","name":"Ahmet Karaman","description":"Pedodontist","expertise_categories":"1","categories_name":"Doktor","profile_image":"http:\/\/www.....net\/app\/assets\/default\/user1.png"},
...

【问题讨论】:

  • 一种更常见的方法是创建模型、解码响应、按所需属性对数组进行排序并重新加载表。考虑尝试一下。 Decode exampleSort array of custom objects by property
  • self.marrDoctorList = DictResponse["data"].arrayValue 在那里排序。如果您使用的是 Swift 4+,建议使用 Codable,并使用任何版本的自定义 Objets/struct 来表示您的数据(即模型),而不仅仅是字典。

标签: arrays json swift sorting tableview


【解决方案1】:

您可以尝试使用这样的排序功能对“self.marrDoctorList”进行排序:

self.marrDoctorList.sorted { $0. name.lowercased() < $1.name.lowercased() } 

例如在您的代码中

        if DictResponse["success"].boolValue
        {
            self.marrDoctorList = DictResponse["data"].arrayValue.sorted { $0. name.lowercased() < $1.name.lowercased() }    

//or
    self.marrDoctorList.sorted { $0. name.lowercased() < $1.name.lowercased() } 

            self.tblDoctors.reloadData()
        }

【讨论】:

  • 添加此代码时出现错误 :( 它说;“JSON”类型的值没有成员“名称”
  • 它应该是一个对象,用 name , doctor_id, desc ext.. 创建一个结构体,并使用 codeable 将其解析为结构体。而不是循环 dictresponse 循环输入你的类的响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2021-12-11
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多