我是为iOS开发的初学者。 在我之前的任务是使用api GitHub获取有关用户要点描述的数据。
想法是这样的:
我在第一个usernameTextField
(viewcontroller
)的LoginViewController
中输入用户名
然后按 searchTapped ,搜索就会触发,并把您带到第二个视图控制器(GistsViewController
)中,其中在tableview
中显示从json
收到的描述
来自json的数据被解码并添加到 gists 数组,然后通过segue传递到GistsViewController
。
但是该项目没有任何错误,尽管它没有按预期运行。.有人可以帮我编辑我的代码吗?
这是我的模特
struct Gists: Codable {
let description: String?
private enum CodingKeys: String, CodingKey {
case description
}
}
这是第一个viewController:
class LoginViewController: UIViewController {
@IBOutlet weak var warningLabel: UILabel!
@IBOutlet weak var usernameTextField: UITextField!
var gists = [String]()
var username: String?
func displayWarningLabel(withText text: String) {
warningLabel.text = text
UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: { [weak self] in
self?.warningLabel.alpha = 1
}) { [weak self] complete in
self?.warningLabel.alpha = 0
}
}
override func viewDidLoad() {
super.viewDidLoad()
warningLabel.alpha = 0
}
@IBAction func unwindToMainScreen(segue: UIStoryboardSegue) {
usernameTextField.text = ""
}
@IBAction func searchTapped(_ sender: UIButton) {
guard let username = usernameTextField.text, username != "" else {
displayWarningLabel(withText: "Information is incorrect")
return
}
guard let gitUrl = URL(string: "https://api.github.com/users/\(username)/gists") else { return }
URLSession.shared.dataTask(with: gitUrl) { (data, response, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let gitData = try decoder.decode([Gists].self, from: data)
DispatchQueue.main.sync {
self.gists.append(gitData.description)
}
} catch let err {
print("Err", err)
}
}.resume()
performSegue(withIdentifier: "GistsSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let dvc = segue.destination as? GistsViewController else { return }
dvc.username = usernameTextField.text
dvc.gists1 = gists
}
}
这是第二个viewController:
class GistsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var username: String!
var gists1 = [String]()
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gists1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
cell.textLabel?.text = gists1[indexPath.row]
self.tableView.reloadData()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
title = username
tableView.tableFooterView = UIView(frame: .zero)
tableView.dataSource = self
tableView.delegate = self
print(gists1)
}
@IBAction func addTapped(_ sender: UIBarButtonItem) {
}
}
我将非常感谢您的帮助!
答案 0 :(得分:0)
对api的调用是异步的,因此请在完成代码块中插入performSegue
guard let gitUrl = URL(string: "https://api.github.com/users/\(username)/gists") else { return }
URLSession.shared.dataTask(with: gitUrl) { (data, response, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let gitData = try decoder.decode([Gists].self, from: data)
DispatchQueue.main.sync {
self.gists.append(contentsOf:gitData)
performSegue(withIdentifier: "GistsSegue", sender: nil)
}
} catch let err {
print("Err", err)
}
}.resume()