Ios 运行代码时,数据不会显示在表视图单元格中
请帮忙!我什么都试过了。如果有人对我如何在表视图单元格中显示数据有任何建议,我将万分感激。我是iOS新手,学习速度非常快。我从一个API获取数据,该API以JSON的形式返回数据,对其进行解析,并使用其表视图单元格创建了我的表视图,但我似乎不知道如何在表视图单元格中打印解析的数据Ios 运行代码时,数据不会显示在表视图单元格中,ios,swift,api,tableviewcell,Ios,Swift,Api,Tableviewcell,请帮忙!我什么都试过了。如果有人对我如何在表视图单元格中显示数据有任何建议,我将万分感激。我是iOS新手,学习速度非常快。我从一个API获取数据,该API以JSON的形式返回数据,对其进行解析,并使用其表视图单元格创建了我的表视图,但我似乎不知道如何在表视图单元格中打印解析的数据 import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet
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数据。获取数据是异步的。因此,您需要在内部添加分派代码。 在这行代码之后:
让episodeNumber=meta[2][“episodeNumber”]!作为!字符串?
加上这个
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()})
编辑:
是
let data = [self.description]
而self.description()
是用于打印调试说明的viewController的说明方法。这就是为什么
cell.textLabel!.text = "\(self.data)"
为您提供
[(函数)]
,因为您刚刚创建了一个包含存储函数的数组。失败的原因是数据操作太多。没有必要使用如此多的变量和不必要地传递数据。在控制台中打印时,您得到了正确的输出,因为您使用了变量“eposodenumber”和“description”
以及在变量“data”中获取错误的数据
所以更好的做法是,您应该使用epiodeNumber和description变量来打印单元格中的数据
cell.textLabel!.text = "Episode Number: \(self.episodeNumber)\n" + "Description: \(description)"
但为此,必须使变量epiodenumber
成为全局变量
所以在函数外部声明它
var episodeNumber = String()
并从第行中删除let
关键字
let episodeNumber = meta[2]["episodeNumber"]! as! String?
你必须添加一些self.
关键字,编译器会建议你使用这些关键字,这样你就不用担心了,只要继续双击这些建议即可
现在,您的代码看起来可以正常运行并获得所需的输出。是!我提取了我的数据。我同意我的代码很混乱。我只是想让它发挥作用。谢谢你的回答!我得到一个错误提示:对成员“tableView”的不明确引用编辑了我的答案,在视图控制器中拖放表不是一个好的做法。然而,假设您必须这样做,您需要添加一个表的委托。非常感谢您在这方面的帮助,@jo3birdtalk!我在代码的修复程序中进行了编辑,现在我的TableView单元格显示:“[(函数)]”…但是我仍然存在一个问题,即我在输出中接收到的数据证明解析的信息是正确的,但我似乎找不到在我的table view单元格中显示它的方法。]:你对此有什么建议吗?信息的打印有效吗<代码>打印(“剧集编号:\(剧集编号!)\n“+”描述:\(描述!)”什么是
self.data
?从代码中,您只需要一行,该行将在文本标签中包含self.data
。@vikingosegundo噢,这是我忘记编辑的注释。我刚刚更新了它。@vikingosegundo是的,我只在一个单元格中显示我输出的信息,即我从API获得的一个系列的集号和描述。当我的输出框中有我请求的正确数据时,我的单元格此时只显示“[(函数)]。它终于起作用了。你是救命恩人。真是太感谢你了…一千次谢谢你@杰梅塔
cell.textLabel!.text = "Episode Number: \(self.episodeNumber)\n" + "Description: \(description)"
var episodeNumber = String()
let episodeNumber = meta[2]["episodeNumber"]! as! String?