我有一个tableView,其中填充了来自JSON数组的信息。当我点击那个单元格时,它会分割成一个DetailsViewController,它应该显示在哪里
我已经有#1工作,#2是我需要帮助的(我确定在我弄清楚之后我可以轻松地做id标签)。我正在使用didSelectRowAtIndexPath来更新时间标签,程序运行正常,但时间标签没有更新信息。标签甚至没有显示出来。
这是我的tableViewController文件中的代码:
class EarthTableViewController: UITableViewController {
var info = [AppModel]()
func getEarthquakeInfo(completion: (results : NSArray?) ->Void ){
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if let JsonArray = json.array {
for appDict in JsonArray {
var ids: String? = appDict["id"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var information = AppModel(idEarth: ids, title: title, time: time)
self.info.append(information)
completion(results: self.info)
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
getEarthquakeInfo { (info) in
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
let infoArray = self.info
cell.textLabel!.text = info[indexPath.row].title
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "SEGUE" {
let vc = segue.destinationViewController as DetailsViewController
let cell = (sender as UITableViewCell)
let title = cell.textLabel!.text
vc.titleData = title
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let time = info[indexPath.row].time
let destinationVC = DetailsViewController()
destinationVC.timeData = time
}
我的DetailsViewController文件:
class DetailsViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var idLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
var titleData: String!
var idData: String!
var timeData: String!
override func viewDidLoad() {
super.viewDidLoad()
let earthInfo = EarthTableViewController()
titleLabel.text = titleData
idLabel.text = idData
timeLabel.text = timeData
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
以防这是我使用的AppModel.swift文件:
class AppModel: NSObject, Printable {
let idEarth: String
let title: String
let time: String
override var description: String {
return "ID: \(idEarth), TITLE: \(title), TIME: \(time), \n"
}
init(idEarth: String?, title: String?, time: String?) {
self.idEarth = idEarth ?? ""
self.title = title ?? ""
self.time = time ?? ""
}
}
任何帮助都将不胜感激。
更新答案:
利用下面人们的评论帮助,我能够找到自己问题的答案。事实证明,我甚至不需要使用didSelectRowAtIndexPath。我之所以添加它的原因是因为当我在prepareForSegue中尝试let time = info[indexPath.row].time
时它没有用,因为我没有添加我需要的代码。现在所有标签都正确显示信息。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "SEGUE" {
let vc = segue.destinationViewController as DetailsViewController
let cell = (sender as UITableViewCell)
let title = cell.textLabel!.text
vc.titleData = title
if let indexPath = self.tableView.indexPathForSelectedRow(){
let time = info[indexPath.row].time
println("\(time)")
vc.timeData = time
let id = info[indexPath.row].idEarth
vc.idData = id
}
}
}
答案 0 :(得分:3)
这似乎是关于如何在两个视图控制器之间传递数据的一个简单错误。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "SEGUE" {
let vc = segue.destinationViewController as DetailsViewController
let cell = (sender as UITableViewCell)
let title = cell.textLabel!.text
vc.titleData = title
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let time = info[indexPath.row].time
let destinationVC = DetailsViewController()
destinationVC.timeData = time
}
问题在于您将信息发送到未呈现的UIViewController。在didSelectRowAtIndexPath:
中,您正在初始化视图控制器并正确地将数据发送给它,但该特定实例从未显示过。要解决此问题,您有几个选择。您可以选择继续使用动作Segue并使用performSegueWithIdentifier:
&传输segue中的数据。 prepareForSegue:
,或者您可以选择使用instantiateViewControllerWithIdentifier:
并手动显示所述视图控制器。