Ich hatte 2 Programme, das erste las JSON online und speicherte es im Array. Das zweite Programm vervollständigen UITableView
mit Array, aber wenn ich beitreten nicht funktionieren.ViewTable liest Array nicht (IOS)
Wenn ich debuggen schaue ich zuerst die Tabelle tun, aber ich habe die Sohn-Methode zuerst.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var TableData:Array<String> = Array <String>()
let textCellIdentifier = "TextCell"
override func viewDidLoad() {
super.viewDidLoad()
//--------------------------------------------------------------------------------------------------
let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
if let stations = json["stations"] as? [[String: AnyObject]] {
for station in stations {
if let name = station["stationName"] as? String {
if let year = station["buildYear"] as? String {
self.TableData.append(name + "[" + year + "]")
}
}
}
}
}catch {
print("Error with Json: \(error)")
}
/* for element in TableData {
print(element)
}*/
}
}
task.resume()
//--------------------------------------------------------------------------------------------------
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
cell.textLabel?.text = TableData[row]
return cell
}
}
Auf meinem Emulator sehen nur den leeren Tisch
Erwartete Erklärung, wie kann ich erklären? –
Klingt so, als ob Sie es an einer Stelle platzieren, die außerhalb des Geltungsbereiches der Methode liegt. – DogCoffee
Variablennamen nicht groß schreiben, TableData ... sollte tableData sein. Sie können auch so deklarieren - var tableData = [String]() ... viel sauberer imo – DogCoffee