Ich verwende einen Dienst in einem Hintergrund-Thread, um eine Post-Anfrage abzurufen. Dann benutze ich NSJSONSerialization, um das zu einem Array zu machen. Ich führe das Array durch, um ein Array von Teams zu erstellen. Dann gehe ich zurück zur Hauptwarteschlange und rufe den Beendigungshandler auf.IOS 9 TableViewCell nicht sichtbar bis ausgewählt
Team:
class Team
{
private (set) var id: Int
private (set) var city: String
private (set) var name: String
private (set) var abbreviation: String
init(data: JSONDictionary)
{
id = data["team_id"] as? Int ?? 0
city = data["city"] as? String ?? ""
name = data["team_name"] as? String ?? ""
abbreviation = data["abbreviation"] as? String ?? ""
}
}
Service:
func getTeams(urlString: String, completion: [Team] -> Void)
{
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let session = NSURLSession(configuration: config)
let url = NSURL(string: urlString)!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
print(data)
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? JSONArray {
var teams = [Team]()
for team in json {
let team = Team(data: team as! JSONDictionary)
teams.append(team)
}
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
dispatch_async(dispatch_get_global_queue(priority, 0)) {
dispatch_async(dispatch_get_main_queue()) {
completion(teams)
}
}
}
} catch {
print("error in NSJSONSerialization")
}
}
}
task.resume()
}
ich dann versuchen, Daten zu verwenden, um einen Tableview zu füllen. Ich schleife auch durch und drucke alle Teamnamen erfolgreich auf die Konsole aus. Das Problem, das ich habe, bevölkert die tableView, aber alles ist ganz weiß. Ich kann keinen Text von meinen Etiketten sehen, bis ich ihn berühre. Während die Tabellenzelle ausgewählt ist, kann ich den Inhalt der Etiketten sehen, die schwarz sind. Aber wenn ich einen anderen anfasse, wird nur das aktuell ausgewählte Etikett angezeigt. Es scheint, dass sie alle sichtbar sein sollten, sobald die Daten geladen sind.
benutzerdefinierte Zelle:
class TeamTableViewCell: UITableViewCell {
var team: Team? {
didSet {
updateCell()
}
}
@IBOutlet weak var title: UILabel!
@IBOutlet weak var abbreviation: UILabel!
func updateCell()
{
title.text = team?.name ?? ""
abbreviation.text = team?.abbreviation ?? ""
}
}
Controller:
var teams = [Team]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Teams"
let service = NBAService()
service.getTeams("https://probasketballapi.com/teams?api_key=\(Constants.API.APIKey)", completion: didLoadTeams)
}
func didLoadTeams(teams: [Team])
{
self.teams = teams
tableView.reloadData()
// This actuall works returns an list of team names to the console.
for team in teams {
print("Team: \(team.name)")
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(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 teams.count
}
struct Storyboard {
static let TeamCell = "TeamCell"
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.TeamCell, forIndexPath: indexPath) as! TeamTableViewCell
// Configure the cell...
cell.team = self.teams[indexPath.row]
return cell
}
Wenn ich die Namen der Teams auf der Konsole drucken, die fein druckt, damit ich weiß, dass ich erfolgreich die Daten wieder aus der Anfrage bekommen haben. Und jeweils ein Team ist sichtbar, wenn die Zelle ausgewählt ist. Was bin ich