gpt4 book ai didi

php - 如何在Swift 3.0中将JSON数据提取到UIPickerView中?

转载 作者:行者123 更新时间:2023-12-01 19:56:48 26 4
gpt4 key购买 nike

我的问题是,如何在Swift 3.0中将数据从PHP提取到UIPickerView中?

目前,我已经为UIPickerView创建了这些代码以创建下拉列表。现在,我只能基于xcode var department = ["ICTD","FAD","PSD"]内部的变量声明显示下拉列表值

dropdown.swift

import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet var departmentLbl: UITextField!
@IBOutlet var dropdownLbl: UIPickerView!
@IBOutlet var outputLbl: UILabel!

@IBOutlet var user_idLbl: UILabel!

var department = ["ICTD","FAD","PSD"]

var user_id: String!

override func viewDidLoad() {
super.viewDidLoad()

user_id = "ID001" // these value that need to be past to PHP

let url = URL(string: "http://localhost/getdepartment.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let LoginDataToPost = "user_id=\(user_id!)"
request.httpBody = LoginDataToPost.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil { return }
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String] {
DispatchQueue.main.async {
let display = Int(json["display"]!)
let realname = json["real_name"]
let department = json["dept"]

if(display == 1) {
// dropdown list value display here
return
}
else { return }
}
}
else { }
}
catch {}
}
})
task.resume()
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

var countrows : Int = department.count
if pickerView == dropdownLbl {
countrows = self.department.count
}
return countrows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if pickerView == dropdownLbl {
let titleRow = department[row]
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if pickerView == dropdownLbl {
self.departmentLbl.text = self.department[row]
self.dropdownLbl.isHidden = true
}
}

func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField == self.departmentLbl) {
self.dropdownLbl.isHidden = false
}
}

}

我有这些PHP代码可以提供

真实姓名部门

基于x代码中的 user_id

getdepartment.php
<?php
$connect = mysqli_connect("","","","");
global $connect;

if (isset($_POST['user_id'])) {

$user_id = $_POST['user_id'];

$sql = "SELECT * FROM table WHERE user_id ='$user_id'";
$result = mysqli_query($connect,$sql);
if($result && mysqli_num_rows($result)>0){
while ($row = mysqli_fetch_array($result)) {

$real_namedb = $row['real_name'];
$dept_db = $row['dept'];

$output = array('real_name' => $real_namedb, 'dept' => $dept_db);
echo json_encode($output);
}
mysqli_free_result($result);
}
else { }
}
?>

这些PHP提供了JSON数据的输出,如下所示:
 {"display":"1","real_name":"NAME TEST 1","dept":"ICTD"}
{"display":"1","real_name":"NAME TEST 2","dept":"ICTD"}

感谢有人可以帮助您。

谢谢。

最佳答案

我相信您应该根据从服务器收到的值来创建下拉列表。根据您在问题中给出的代码,我观察到您没有将服务器中获得的部门名称添加到阵列中。

您可以进行以下更改并观察:

if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
{
DispatchQueue.main.async
{
let display = Int(json["display"]!)
let realname = json["real_name"]
let departmentName = json["dept"]

if(display == 1) {
// dropdown list value display here
self.department.append(departmentName)
self.dropdownLbl.reloadAllComponents() // this is reference to your pickerView. Make it global and use it
return
}else { return }
}
}

请让我知道它是否解决了问题...

关于php - 如何在Swift 3.0中将JSON数据提取到UIPickerView中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com