私はAlamofire要求からのデータでグループ化されたテーブルを移入しようとしてきました。私はこれまで、配列の静的データをテーブルに埋め込んでいましたが(写真のように)、何時間も試してみて、試してみましたが、JSONデータの使い方を考えていませんでした。それはあまり差をつけるべきではありませんが、記録のためにはこれはSwift 3です。移入グループ化されたAlamofireから表とSwiftyJSON
ご協力いただければ幸いです。ありがとう。ここで
素晴らしい働いている私の静的コード、です。
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//static Data Here:
var array = [ ["Clients", "John Doe", "Joe Bloggs"],["Departments", "HR", "Admin", "Finance"]]
let cellReuseIdentifier = "cell"
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
tableView.delegate = self
tableView.dataSource = self
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array[section].count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:AreasCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! AreasCustomCell
cell.areasPreview.contentMode = .scaleAspectFit
request(.GET, "https://url.here.com", parameters: ["file": "default.png"]).response { (request, response, data, error) in
cell.areasPreview.image = UIImage(data: data!, scale:0.5)
}
cell.areasCellLabel.text = array[indexPath.section][indexPath.row + 1]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return array[section][0]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
//More things planned here later!
}
}
私が扱っているJSONのフォーマットもここにあります。
content = {
clients = (
{
desc = "Description here";
group = client;
id = "group_7jsPXXAcoK";
name = "John Doe";
},
{
desc = "Description here";
group = client;
id = "group_19MrV7OLuu";
name = "Joe Bloggs";
}
);
departments = (
{
desc = "Description here";
group = department;
id = "group_PhAeQZGyhx";
name = "HR";
},
{
desc = "Description here";
group = department;
id = "group_RMtUqvYxLy";
name = "Admin";
},
{
desc = "Description here";
group = department;
id = "group_T50mxN6fnP";
name = "Finance";
}
);
};
state = success;
これまで私は正しい方向にステップしているJSONデータを保持する新しいクラスを追加しました。
class Group {
let id : String
let name : String
let desc : String
let group : String
init(dictionary : [String : AnyObject]) {
id = dictionary["id"] as? String ?? ""
desc = dictionary["desc"] as? String ?? ""
name = dictionary["name"] as? String ?? ""
group = dictionary["group"] as? String ?? ""
}
}
最後に、viewDidLoadから呼び出されるJSONデータを最初に取得する機能を示します。
func getData()
{
let defaults = UserDefaults.standard()
let token = defaults.string(forKey: defaultsKeys.userToken)
let email = defaults.string(forKey: defaultsKeys.userEmail)
request(.POST, "https://url.here.com/api/v2.php", parameters: ["type": "areas", "uEmail": email!, "token": token!])
.responseJSON { response in
var json = JSON(response.result.value!)
let state = json["state"].stringValue
if(state == "error"){
print(json["message"].stringValue)
} else {
print(response.result.value)
//Send JSON data here to Table!
}
}
}
レスポンスのおかげで、レスポンスがいつ終了するかを知ることが重要になった理由を理解しました。まず、performAPICallをPOSTリクエストにするにはどうすればよいですか。私は、JSONを取得するためにいくつかの値を送る必要があります。 最後に、画像に表示されているように、グループクラスをどのようにテーブルに入れるのですか?つまり、別々のグループ化されたセルを使用していますか? ありがとうございました:) – Aloogy