简体   繁体   English

如何在同一个表格视图上显示来自 json 的两个不同数据

[英]How to Display two different data from json on same tableview

I am trying to put 2 diff data from same json on same tableview but I am not able to do, In every case.我试图将来自同一个 json 的 2 个差异数据放在同一个 tableview 上,但在每种情况下我都做不到。 I can put only one.我只能放一个。 I want to display.name and .email at the same time我想同时显示.name 和.email

Thanks谢谢

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userInfo = [UserData]()

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        JsonDownload
            {

                self.tableView.reloadData()
           }
        tableView.delegate = self
        tableView.dataSource = self

    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = userInfo[indexPath.row].name


        return cell
    }

    func JsonDownload(completed: @escaping () -> ()) {

        let source = URL(string: "https://jsonplaceholder.typicode.com/users")

        URLSession.shared.dataTask(with: source!) { (data, response, error) in

            if let data = data {
                do
                {
                    self.userInfo = try JSONDecoder().decode([UserData].self, from: data)

                    DispatchQueue.main.async
                    {
                        completed()
                    }
                }
                catch
                {
                    print("Json Error")
                }
            }
        }.resume()
    }
}

here is how you can display.name and.email at the same time.. but instead of creating new cell each time.. use deque这是您可以同时显示.name 和.email 的方法。但不是每次都创建新的单元格。使用 deque

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

         if  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"){
            cell.textLabel?.text = userInfo[indexPath.row].name
            cell.detailTextLabel?.text = userInfo[indexPath.row].email

            return cell
          }
            return UITableViewCell()
        }

If you want to use the default cell provided by Apple.如果要使用 Apple 提供的默认单元格。 You can you textLabel and detailTextLabel .你可以textLabeldetailTextLabel Another better way is creating your custom tableviewcell.另一种更好的方法是创建自定义 tableviewcell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 TableView 中显示来自 API 的 JSON 数据? - How to display JSON data from API in TableView? 如何从另一个表视图刷新表视图中的数据 - How to refresh data in a tableview from a different tableview 如何从存储在tableView的Core数据中的用户数组中获取并显示不同的用户数据? - How to fetch and display different user data from array of users stored in Core data in tableView? 如何在DetailsView上显示TableView中的数据 - How to display data from TableView on a DetailsView 如何通过在iOS中执行JSON Pasing一次在tableview上显示不同的图像? - How to display different images on tableview at a time by doing JSON Pasing in iOS? 如何对两个不同的表视图使用相同的表视图单元格? - How to use same tableview cell for two different tableviews? 如何在同一UITableViewVibrantCell中加载两个不同的动态tableview单元格 - how to load two different dynamic tableview cells in same UITableViewVibrantCell 如何自定义cellForRowAtIndexPath TableView方法以根据采取的措施显示来自不同来源的数据? - How to customize cellForRowAtIndexPath TableView method to display data from different sources depending on action taken? TableView跳过一些单元格以显示JSON中的数据 - TableView skips a few cells to display data from JSON tableView无法显示以json格式从Web解析的数据 - tableView unable to display data parsed from the web in json format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM