我很难从json结果中填充tableview。我的代码如下(道歉但它似乎不想将前两行作为代码:/):
导入UIKit
类ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad()
{
super.viewDidLoad()
let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
let data = NSData(contentsOfURL: url!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
let teamsArray = json["teams"] as NSArray
print("Team List : \(teamsArray)")
for dic in teamsArray
{
let teamname = dic["name"] as NSString
let code = dic["code"] as NSString
println("Team Name, \(teamname) : Code, \(code)")
}
// self.tableViewObject.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
@IBOutlet weak var tableViewObject: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return teamsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel!.text = teamsArray[indexPath.row]
return cell
}
}
正在抱怨两次"使用未解决的标识符' teamsArray'两次。首先是:
return teamsArray.count
然后在:
cell.textLabel!.text = teamsArray [indexPath.row]
有人可以帮助我将我的JSON链接到我的桌面视图,请帮助我解决上面提到的错误或让我朝着正确的方向前进。
值得注意的是,当我在没有任何桌面视图提示的空白视图上使用代码时,我在控制台中获得了完美的结果:
let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
let data = NSData(contentsOfURL: url!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
let teamsArray = json["teams"] as NSArray
print("Team List : \(teamsArray)")
for dic in teamsArray
{
let teamname = dic["name"] as NSString
let code = dic["code"] as NSString
println("Team Name, \(teamname) : Code, \(code)")
}
我是stackoverflow的新手,之前被告知我的问题不够具体。如果这仍然太模糊,请告诉我,我会尽力改进。
非常感谢,艾伦。
答案 0 :(得分:1)
这样做......
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableViewObject: UITableView!
var teamsArray = NSArray()
override func viewDidLoad()
{
super.viewDidLoad()
self.tableViewObject.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
let data = NSData(contentsOfURL: url!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
teamsArray = json["teams"] as NSArray
print("Team List : \(teamsArray)")
self.tableViewObject.reloadData()
for dic in teamsArray
{
let teamname = dic["name"] as NSString
let code = dic["code"] as NSString
println("Team Name, \(teamname) : Code, \(code)")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return teamsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel!.text = self.teamsArray.objectAtIndex(indexPath.row).objectForKey("name") as? NSString
return cell
}