Ios TableView不显示数据
我正试图用从GET请求收到的一些数据填充我的Ios TableView不显示数据,ios,json,swift,uitableview,Ios,Json,Swift,Uitableview,我正试图用从GET请求收到的一些数据填充我的UITableView。我不知道为什么,但如果在我的numberofrowsin部分中,我返回一个特定值(例如,8),它表示信息。如果y返回由请求填充的my数组的值,UITableView不会添加任何行: class DashboardTableViewController: UITableViewController { var userData = [String]() var secondUserData = [String](
UITableView
。我不知道为什么,但如果在我的numberofrowsin部分
中,我返回一个特定值(例如,8),它表示信息。如果y返回由请求填充的my数组的值,UITableView
不会添加任何行:
class DashboardTableViewController: UITableViewController {
var userData = [String]()
var secondUserData = [String]()
var name: String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
/* This function gets the user account information from a JSON in a simple request */
private func loadUserData(completion: @escaping (_ myArray: [String]) -> Void) {
let session = Twitter.sharedInstance().sessionStore.session()
let client = TWTRAPIClient.withCurrentUser()
let userInfoURL = "https://api.twitter.com/1.1/users/show.json"
let params = ["user_id": session?.userID]
var clientError : NSError?
let request = client.urlRequest(withMethod: "GET", url: userInfoURL, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if connectionError != nil {
print("Error: \(connectionError)")
}
do {
let json = JSON(data: data!)
if let userName = json["name"].string,
let description = json["description"].string,
let followersCount = json["followers_count"].int,
let favouritesCount = json["favourites_count"].int,
let followingCount = json["friends_count"].int,
let lang = json["lang"].string,
let nickname = json["screen_name"].string {
self.userData.append(userName)
self.userData.append(nickname)
self.userData.append(String(followersCount))
self.userData.append(String(followingCount))
self.userData.append(String("22"))
self.userData.append(lang)
self.userData.append(description)
self.userData.append("No country")
completion(self.userData)
}
}
}
}
/* This closure helps us to fill the labels once the request has been finished in loadUserData */
func manageUserData(label: UILabel, index: Int) {
loadUserData {
(result: [String]) in
label.text = result[index]
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.userData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowNumber = indexPath.row
let cellIdentifier = "TableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCellController else {
fatalError("The dequeued cell is not an instance of TableViewCellController.")
}
switch rowNumber {
case 0:
cell.titlePlaceholder.text = "Name:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 1:
cell.titlePlaceholder.text = "Nickname:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 2:
cell.titlePlaceholder.text = "Followers:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 3:
cell.titlePlaceholder.text = "Following:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 4:
cell.titlePlaceholder.text = "Tweets:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 5:
cell.titlePlaceholder.text = "Language:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 6:
cell.titlePlaceholder.text = "Biography:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
case 7:
cell.titlePlaceholder.text = "Country:"
manageUserData(label: cell.valuePlaceholder, index: rowNumber)
default:
cell.titlePlaceholder.text = "?"
cell.valuePlaceholder.text = "?"
}
return cell
}
}
知道为什么会这样吗?
非常感谢
更新强>
我在loadUserData()函数中添加self.tableView.reloadData()after completion()解决了这个问题。希望有帮助 您必须在收到请求后重新加载表视图数据
[self.tableView reloadData];
您必须在收到请求后重新加载表视图数据
[self.tableView reloadData];
Swift解决方案:
tableView.reloadData()
如果不是从主线程调用它:
DispatchQueue.main.async {
self.tableView.reloadData()
}
Swift解决方案:
tableView.reloadData()
如果不是从主线程调用它:
DispatchQueue.main.async {
self.tableView.reloadData()
}
在
override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableView单元格{
的开头添加一个断点,以检查是否调用了此方法。这与您的请求有关。请尝试在let json=json(数据:data!)之后立即打印json
并查看接下来会发生什么只有当我在override func tableView(\uTableView:UITableView,numberOfRowsInSection:Int)->IntTrytableView.reloadData()中返回一个特定值时,它才会同时执行这两项操作。
从API下载完数据后,已经尝试使用self.tableView.realoadData()如果我在numberOfRowsInSection函数中返回数组的反计数,并在override func tableView(uTableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell的开头添加一个断点,它仍然不会加载它{
检查此方法是否被调用是关于您的请求的。请尝试在让json=json(data:data!)
之后立即打印json,并查看接下来会发生什么只有在我在override func tableView(tableView:UITableView,numberOfRowsInSection:Int)中返回特定值的情况下,它才会同时执行这两项操作->IntTrytableView.reloadData()
从API下载完数据后,已经尝试使用self.tableView.realoadData(),如果我返回numberOfRowsInSection函数中数组的计数,它仍然不会加载。已经尝试使用self.tableView.realoadData()如果我在numberOfRowsInSection函数中返回数组的反计数,它仍然不会加载它。在代码中,您在哪里重新加载tableView?我已经尝试在完成后在执行请求的方法中执行此操作,然后您必须使用DispatchQueue.main.asyncIt在我的答案中的代码。它仍然不起作用。这真的很奇怪。不能理解为什么只有当我在numberOfRowsInSection函数中返回一个特定的数字时,它才会对数据收费。我已经尝试使用self.tableView.realoadData()如果我在numberOfRowsInSection函数中返回数组的反计数,它仍然不会加载它。在代码中,您在哪里重新加载tableView?我已经尝试在完成后在执行请求的方法中执行此操作,然后您必须使用DispatchQueue.main.asyncIt在我的答案中的代码。它仍然不起作用。这真的很奇怪。不能理解为什么只有当我在numberOfRowsInSection函数中返回一个特定的数字时,它才会对数据收费。我已经尝试使用self.tableView.realoadData(),而如果我在numberOfRowsInSection函数中返回数组的计数,它仍然不会加载数据。我已经尝试使用self.tableView.realoadData()了如果我在numberOfRowsInSection函数中返回数组的反计数,它仍然不会加载它