【问题标题】:How to load data into table view如何将数据加载到表视图中
【发布时间】:2019-12-01 05:20:28
【问题描述】:

如何将数据加载到我的UITableView?谁能帮我解决这个问题: 为什么不加载到 cellForRowAt

  • pPlandDetailId ==>可选(1)
  • 司机 ==> 司机姓名:Boonma,Pongpat
  • carRegistrtation ==> 汽车注册:60-7620
  • StoreLocation ==> ["S.Suratani"]
  • pDepartureDateTime ==> ["18/01/2019 20:00"]
import UIKit

class StoreListMainViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{

// TableView
    @IBOutlet weak var tableView: UITableView!
    var StoreLocation: [String] = []
    var getDepartureTime: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        createTableiew()
    }//Main Method

     //  TableView
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? cellTableViewCell
        cell!.Location.text = StoreLocation[indexPath.row]
        cell!.DepartureTime.text = getDepartureTime[indexPath.row]
        print("cell!.Location.text ==>\(String(describing: cell!.Location.text) )")
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "gotoTempMonitorStoreList") as? StoreListTempMonitorViewController
        self.navigationController?.pushViewController(vc!, animated: true)
    }

   //  Create UI Table view
    func createTableiew() -> Void {
        tableView.delegate = self
        tableView.dataSource = self
    }//createTableiew
import UIKit

class cellTableViewCell: UITableViewCell {

    @IBOutlet weak var Location: UILabel!
    @IBOutlet weak var DepartureTime: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

【问题讨论】:

  • 你在哪里查看didload 在你获取数据后你是否批准了你的tableview委托,你应该重新加载你的tableView
  • 从您的代码看来,您有多个问题可能导致它无法加载数据。在cellForRowAt 中,您将两个值设置为两个标签,但您用作数据源的StoreLocation 数组只是一个字符串数组。首先,你能告诉我jsonResponsePlanDetail 是什么吗?也许可以在这里展示一个它的结构示例?
  • @Isuru 我更新编码请再次建议我
  • @Nardanong.s 我还没有看到jsonResponsePlanDetail 是什么?顾名思义,它是一个 JSON 响应。你能展示一下它的样子吗?
  • @Isuru 我已经更新了代码并显示了 jsonResponsePlanDetail。请给我建议。为什么日期无法加载到 cellForRowAt

标签: ios json swift uitableview


【解决方案1】:

在你收到数据的地方重新加载tableview tableView.reload()

【讨论】:

    【解决方案2】:

    我建议您先获取数据,然后再尝试重新加载表格视图。 一旦您将数组自动设置为表格值,只要您将对象的属性编码为单元格的 Outlet 值,它们就会显示在表格中。

    【讨论】:

      【解决方案3】:

      由于您没有使用UITableViewController,您需要为您的tableView 对象设置一个委托和一个数据源。 看起来像这样:

      override func viewDidLoad() {
            tableView.dataSource = self
            tableView.delegate = self
      }
      

      就是这样。享受

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多