RDowns RDowns - 1 month ago 9
Swift Question

What is the reason for this UITableView not showing up on my view controller?

I have a second view controller (not my main one), "ShowCommunities" I have a TableView which is to be populated with data from an API call.

The table isn't showing up at all.

I have put a

print
command inside the function that counts how many rows there are and it is reporting none.

I have printed the output of my API call and that returns the following:


names: [Optional("Steve Jones"), Optional("Brad Walker")]


I have copied the same functions and set up that I used for a previous table on my main view controller, "View Controller" and I am not getting any errors...

But I am obviously missing something.

This is my code:

import UIKit

class ShowCommunityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var membersTableView: UITableView!

@IBOutlet weak var communityName: UILabel!
var communityIsCalled: String?
var comIds = [String]()
var communityId: Int?

var communityPlayerIds = [String]()
var selectedCellTitle: String?
var cellId: Int?

var communityPlayers = [String?]()

override func viewDidLoad() {
super.viewDidLoad()

communityName.text = communityIsCalled
self.membersTableView.delegate = self
self.membersTableView.dataSource = self
membersTableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print (self.communityPlayers.count)
return self.communityPlayers.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let title = self.communityPlayers[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedCellTitle = self.communityPlayers[indexPath.row]
cellId = indexPath.row
}

override func viewDidAppear(_ animated: Bool) {
let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/specificCommunity.php?");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "id=\(comIds[communityId!])";

request.httpBody = postString.data(using: String.Encoding.utf8);

let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

DispatchQueue.main.async
{

if error != nil {
print("error=\(error)")
return
}

do{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]

if let arr = json?["players"] as? [[String:String]] {
self.communityPlayerIds = arr.flatMap { $0["id"]!}
self.communityPlayers = arr.flatMap { $0["user_name"]!}

print ("names: ",self.communityPlayers)
}

} catch{
print(error)
}
}
}
task.resume()
membersTableView.reloadData()

}

}


I am not even getting an blank cells.

The
print (self.communityPlayers.count)
command outputs:


0
0
0
0
0


Any ideas?

Answer

put membersTableView.reloadData() inside DispatchQueue.main.async after self.communityPlayers get assigned