【发布时间】:2015-07-06 15:13:36
【问题描述】:
我有一个 tableView 填充了来自 JSON 数组的信息。当我点击那个单元格时,它会进入一个应该显示的 DetailsViewController
- 显示被点击单元格文本的标签(在我的例子中是标题或条目名称)
- 显示与该单元格标题对应的时间的标签
- 一个标签,显示对应于该单元格标题的 id。所有这些信息(例如时间、id)都来自我的 JSON 数组。
我已经让#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
}
}
}
【问题讨论】:
-
标签在 Interface Builder 中是否有任何文字?如果答案是否定的,我有一个答案给你。
-
确实如此,对不起。我刚刚检查过。
-
收到您的第二条评论后,我不能确定我的回答是否足够 - 但我相信它仍然可能有效,所以我会发布它。带上一粒盐。
标签: json uitableview swift didselectrowatindexpath