Swift 为什么我的tableview不显示任何内容?
目前,我已经尝试设置一个json api来返回用户的所有关注者用户名。我遇到了一个问题,如果我尝试在tableview函数(和调度队列)的开头添加一个断点,它们将不起作用,我不知道为什么。我所有的其他断点都可以工作。无论如何,我想在这里完成的是将每个用户的关注者的用户名返回到不同的单元格中。我尝试过互联网上的不同解决方案,但没有一个有效。我尝试过测试下面的代码,但所有的单元格都是空的。这是我的一些代码,任何人都可能知道我做错了什么 视图控制器Swift 为什么我的tableview不显示任何内容?,swift,Swift,目前,我已经尝试设置一个json api来返回用户的所有关注者用户名。我遇到了一个问题,如果我尝试在tableview函数(和调度队列)的开头添加一个断点,它们将不起作用,我不知道为什么。我所有的其他断点都可以工作。无论如何,我想在这里完成的是将每个用户的关注者的用户名返回到不同的单元格中。我尝试过互联网上的不同解决方案,但没有一个有效。我尝试过测试下面的代码,但所有的单元格都是空的。这是我的一些代码,任何人都可能知道我做错了什么 视图控制器 import UIKit class Follow
import UIKit
class FollowerListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return followers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
parseFollowers() // Second breakpoint
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
tableView.dataSource = self;
let follower = followers[indexPath.row]
cell.textLabel!.text = follower.username
DispatchQueue.main.async {
self.tableView.reloadData()
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
parseFollowers()
}
struct Root: Codable {
let followers : FollowerData
}
struct FollowerData: Codable {
enum CodingKeys: String, CodingKey {
case username = "username"
}
let username : String
}
@IBOutlet weak var tableView: UITableView!
var followers = [FollowerData]()
func parseFollowers() {
let decoder = JSONDecoder()
do {
let url = URL(string: "http://localhost:3000/api/v1/profiles/1.json")
let jsonData = NSData(contentsOf: url!)
let output = try decoder.decode(Root.self, from: jsonData! as Data)
} catch {
print(error.localizedDescription)
showNoResponseFromServer()
return
}
DispatchQueue.main.async { // Second breakpoint
self.tableView.reloadData()
}
}
func showNoResponseFromServer() {
let alert = UIAlertController(title: "Error", message: "No response from server. Try again later.", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
{
"username": "test1",
"followers": [
{
"username": "test2"
}
]
}
来自服务器的json代表
import UIKit
class FollowerListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return followers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
parseFollowers() // Second breakpoint
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
tableView.dataSource = self;
let follower = followers[indexPath.row]
cell.textLabel!.text = follower.username
DispatchQueue.main.async {
self.tableView.reloadData()
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
parseFollowers()
}
struct Root: Codable {
let followers : FollowerData
}
struct FollowerData: Codable {
enum CodingKeys: String, CodingKey {
case username = "username"
}
let username : String
}
@IBOutlet weak var tableView: UITableView!
var followers = [FollowerData]()
func parseFollowers() {
let decoder = JSONDecoder()
do {
let url = URL(string: "http://localhost:3000/api/v1/profiles/1.json")
let jsonData = NSData(contentsOf: url!)
let output = try decoder.decode(Root.self, from: jsonData! as Data)
} catch {
print(error.localizedDescription)
showNoResponseFromServer()
return
}
DispatchQueue.main.async { // Second breakpoint
self.tableView.reloadData()
}
}
func showNoResponseFromServer() {
let alert = UIAlertController(title: "Error", message: "No response from server. Try again later.", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
{
"username": "test1",
"followers": [
{
"username": "test2"
}
]
}
移动这条线
tableView.dataSource = self
到viewDidLoad
并更改数据源数组
let output = try decoder.decode(Root.self, from: jsonData! as Data)
self.followers = output.followers
不要使用
让jsonData=NSData(contentsOf:url!)
,因为它会阻止主线程使用Alamofire
或URLsession
在数组中添加值的位置?