【问题标题】:Show data in table view from mysql using alamofire and swiftyjson使用 alamofire 和 swiftyjson 在 mysql 的表视图中显示数据
【发布时间】:2018-10-28 13:31:20
【问题描述】:

我的服务器上有一个 php 脚本,它只是一个基本的 sql SELECT 语句,它从 mysql 数据库中获取一些数据并返回一些行。

我使用 alamofire 和 swiftyjson 将数据打印到控制台,但我想在表格视图中显示它。

由于调用与 tableView 代码不在同一范围内(我认为这就是我收到错误消息“使用未解析的标识符”的原因)

我不确定如何使其成为全局变量,但我想我需要创建一个全局数组变量,但不确定它是否应该为空?

全局:

 let serviceURL = "http://example.com/service.php"

我把它放在这样的函数中:

    func getUsers() {

    Alamofire.request(serviceURL, method: .get).validate().responseJSON { (response) in

        if response.result.isSuccess {

            let userJSON : JSON = JSON(response.result.value!)


            for (index,subJson):(String, JSON) in userJSON  {
                let firstName = subJson["first_name"].string 
                let lastName = subJson["last_name"].string
                print(firstName)
            }

        } else {

            print("Could not get results")
        }
    }
}

我需要以某种方式计算返回的行数

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

   return firstName.count

}

然后实际显示在单元格中

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)

    cell.textLabel?.text = names[indexPath.row]

    return cell
}

更新

import UIKit
import Alamofire
import SwiftyJSON

struct User {
    var firstName: String
    var lastName: String

    private enum CodingKeys: String, CodingKey {

        case firstName = "first_name"
        case lastName = "last_name"
    }
}

class UserTableViewController: UITableViewController {

    var users = [User]()

    let serviceURL = "http://example.com/service.php"


    override func viewDidLoad() {
        super.viewDidLoad()
        getUsers()

    }


    func getUsers() {
        Alamofire.request(serviceURL, method: .get).validate().responseJSON { (response) in
            if response.result.isSuccess {
                let userJSON : JSON = JSON(response.result.value!)

                for (index,subJson):(String, JSON) in userJSON  {
                    let firstName = subJson["first_name"].string
                    let lastName = subJson["last_name"].string
                    let user = User(firstName: firstName!, lastName: lastName!)
                    self.users.append(user)

                }
            } else {
                print("Could not get results")
            }
        }
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)
        let user = users[indexPath.row]
        cell.textLabel?.text = "\(user.firstName) \(user.lastName)"

        return cell
    }
}

【问题讨论】:

  • names 数组包含什么?为什么不在getUserstableView:numberOfRowsInSection 中使用这个数组?
  • 抱歉,名称来自我刚刚创建一个数组以在使用 alamofire 等之前将数据放入表中。它只是一个像这样的数组:let names = ["Bob", "Judy", "Tony"] 但我现在需要它包含数据库中的数据

标签: ios swift swift4 alamofire swifty-json


【解决方案1】:

首先你需要一个结构来保存你的数据

struct User {
    var firstName: String
    var lastName: String
}

然后你需要一个数组来保存来自 json 消息的用户,在你的视图控制器中将它声明为一个属性

var users = [User]()

然后在你的代码中使用它

func getUsers() {
    Alamofire.request(serviceURL, method: .get).validate().responseJSON { (response) in
        if response.result.isSuccess {
            let userJSON : JSON = JSON(response.result.value!)

            for (index,subJson):(String, JSON) in userJSON  {
                let firstName = subJson["first_name"].string 
                let lastName = subJson["last_name"].string
                let user = User(firstName: firstName, lastName: lastName)
                users.append(user)
            }
        } else {
            print("Could not get results")
        }
    }
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)
    let user = users[indexPath.row]
    cell.textLabel?.text = "\(user.firstName) \(user.lastName)"

    return cell
}

【讨论】:

  • 谢谢。但是运行之后表格中没有数据显示。不过我没有收到任何错误。
  • 不幸的是,我无法真正帮助您,因为我对您的项目有很多不了解。尝试调试代码或在关键位置添加打印语句,以确保您拥有预期的数据
  • 我已经在视图控制器中添加了所有代码,以便您可以看到所有内容。这就是我在整个项目中的全部内容(将代码添加到原始问题中。)
  • 你什么时候打电话给getUsers
  • 在 viewDidLoad() 中
猜你喜欢
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多