当我 运行 我的代码时,数据未显示在 table 视图单元格中
Data does not show up in table view cell when i run my code
请帮忙!我已经尝试了一切。如果有人对我如何在 table 视图单元格中显示我的数据有任何建议,我将永远感激不已。我是 iOS 的新手,正在以非常快的速度学习。我从 API 中获取数据,该数据以 JSON 的形式返回数据,对其进行解析,创建我的 table 视图及其 table 视图单元格,但我似乎无法弄清楚如何打印我在 table 视图单元格中解析的数据。
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView! {
didSet {
myTableView.dataSource = self
myTableView.delegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://api.viacom.com/apiKey=someKey")!
let request = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
if let response = response, data = data {
var json: [String: AnyObject]!
do {
json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String : AnyObject]
} catch {
print(error)
}
//2 - Store in model, forloop through them, store into temparray,add to main array?
let episodes = json["response"] as! [String: AnyObject]
let meta = episodes["episodes"] as! [AnyObject]
let description = meta[2]["description"]! as! String?
//let title = meta[2]["title"] as! String?
let episodeNumber = meta[2]["episodeNumber"]! as! String?
dispatch_async(dispatch_get_main_queue(), {
self.myTableView.reloadData()})
data = [episodeNumber!, description!]
print("Episode Number: \(episodeNumber!)\n" + "Description: \(description!)")
} else {
print(error)
}
}
task.resume()
}
let data = [description]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = "\(self.data)"
return cell
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我觉得你的代码很乱。但是,我只是假设您已成功获取 JSON 数据。获取数据是异步的。因此,您需要在其中添加一个调度代码。
在你的这行代码之后:
let episodeNumber = meta[2]["episodeNumber"]! as! String?
添加这个
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()})
编辑:
@IBOutlet weak var myTableView: UITableView! {
didSet {
myTableView.dataSource = self
myTableView.delegate = self // Add This
}
}
let data = [description]
是
的缩写形式
let data = [self.description]
和self.description()
是viewController的描述方法,用于打印调试描述。这就是为什么
cell.textLabel!.text = "\(self.data)"
给你 [(Function)]
,因为你刚刚创建了一个数组,其中包含一个存储函数。
失败的原因是数据操作过多。不需要使用那么多变量和不必要地传递数据。打印时您在控制台中得到正确的输出,因为您使用了变量 "episodeNumber" 和 "description".
print("Episode Number: \(episodeNumber!)\n" + "Description: \(description!)")
并在变量 "data" 中获取错误数据。
更好的是你应该使用 episodeNumber 和 description 变量在 Cell 中打印数据。
cell.textLabel!.text = "Episode Number: \(self.episodeNumber)\n" + "Description: \(description)"
但是为此你必须使变量 episodeNumber
成为全局变量。
所以在函数外声明。
var episodeNumber = String()
并从第
行中删除 let
关键字
let episodeNumber = meta[2]["episodeNumber"]! as! String?
您必须添加一些编译器会建议您的 self.
关键字,因此您不必担心,只需继续双击建议即可。
现在,您的代码看起来很好 运行 并获得所需的输出。
请帮忙!我已经尝试了一切。如果有人对我如何在 table 视图单元格中显示我的数据有任何建议,我将永远感激不已。我是 iOS 的新手,正在以非常快的速度学习。我从 API 中获取数据,该数据以 JSON 的形式返回数据,对其进行解析,创建我的 table 视图及其 table 视图单元格,但我似乎无法弄清楚如何打印我在 table 视图单元格中解析的数据。
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView! {
didSet {
myTableView.dataSource = self
myTableView.delegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://api.viacom.com/apiKey=someKey")!
let request = NSMutableURLRequest(URL: url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
if let response = response, data = data {
var json: [String: AnyObject]!
do {
json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String : AnyObject]
} catch {
print(error)
}
//2 - Store in model, forloop through them, store into temparray,add to main array?
let episodes = json["response"] as! [String: AnyObject]
let meta = episodes["episodes"] as! [AnyObject]
let description = meta[2]["description"]! as! String?
//let title = meta[2]["title"] as! String?
let episodeNumber = meta[2]["episodeNumber"]! as! String?
dispatch_async(dispatch_get_main_queue(), {
self.myTableView.reloadData()})
data = [episodeNumber!, description!]
print("Episode Number: \(episodeNumber!)\n" + "Description: \(description!)")
} else {
print(error)
}
}
task.resume()
}
let data = [description]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = "\(self.data)"
return cell
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我觉得你的代码很乱。但是,我只是假设您已成功获取 JSON 数据。获取数据是异步的。因此,您需要在其中添加一个调度代码。 在你的这行代码之后:
let episodeNumber = meta[2]["episodeNumber"]! as! String?
添加这个
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()})
编辑:
@IBOutlet weak var myTableView: UITableView! {
didSet {
myTableView.dataSource = self
myTableView.delegate = self // Add This
}
}
let data = [description]
是
的缩写形式let data = [self.description]
和self.description()
是viewController的描述方法,用于打印调试描述。这就是为什么
cell.textLabel!.text = "\(self.data)"
给你 [(Function)]
,因为你刚刚创建了一个数组,其中包含一个存储函数。
失败的原因是数据操作过多。不需要使用那么多变量和不必要地传递数据。打印时您在控制台中得到正确的输出,因为您使用了变量 "episodeNumber" 和 "description".
print("Episode Number: \(episodeNumber!)\n" + "Description: \(description!)")
并在变量 "data" 中获取错误数据。
更好的是你应该使用 episodeNumber 和 description 变量在 Cell 中打印数据。
cell.textLabel!.text = "Episode Number: \(self.episodeNumber)\n" + "Description: \(description)"
但是为此你必须使变量 episodeNumber
成为全局变量。
所以在函数外声明。
var episodeNumber = String()
并从第
行中删除let
关键字
let episodeNumber = meta[2]["episodeNumber"]! as! String?
您必须添加一些编译器会建议您的 self.
关键字,因此您不必担心,只需继续双击建议即可。
现在,您的代码看起来很好 运行 并获得所需的输出。