我的桌面视图的didSelectRowAt
功能出现问题。出于某种原因,尽管我的segue标识符和目标视图是正确的,但当我单击该行时,它不会加载任何内容。此外,Xcode告诉我:
无法指定类型的值' Double?'输入' String?'
但我找不到告诉我如何解决此问题的在线资源。作为参考,我想要显示的数据来自嵌套的JSON提要。
JSON Struct
struct PlayerStatsParent:Decodable{
let rankings: [PlayerStats]
}
struct PlayerStats:Decodable {
let personaname: String?
let score: Double?
let solo_competitive_rank: Int?
let avatar: String?
}
单元格选择功能
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? RankDetail {
destination.playerRank = rank[(rankTable.indexPathForSelectedRow?.row)!]
}
}
目标视图控制器代码
import UIKit
class RankDetail: UIViewController {
@IBOutlet var rankLabel: UILabel!
@IBOutlet var scoreLabel: UILabel!
var playerRank:PlayerStats?
override func viewDidLoad() {
super.viewDidLoad()
rankLabel.text = "\(playerRank?.solo_competitive_rank)"
scoreLabel.text = playerRank?.score
}
答案 0 :(得分:1)
同时检查你的选择方法。应该是这个。
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
不是这个;
Event.where(id: @events.map(&:id))
.includes(
:user,
:event_category,
task: [:document_tasks, task_followers: :follower]
)
}
如果选择单元格。
答案 1 :(得分:0)
看起来RankDetail
类中出现错误。
这是一个简单的解决方法。
class RankDetail: UIViewController {
@IBOutlet var rankLabel: UILabel!
@IBOutlet var scoreLabel: UILabel!
var playerRank:PlayerStats?
override func viewDidLoad() {
super.viewDidLoad()
rankLabel.text = "\(playerRank?.solo_competitive_rank)"
// scoreLabel.text if of type String?, so it can't store Double? directly
// scoreLabel.text = playerRank?.score
// converting to String? should fix it
scoreLabel.text = "\(playerRank?.score)"
}
编辑:
使用以下代码显示ViewController
。省去了担心segues的麻烦。
if let viewController = storyboard?.instantiateViewController(withIdentifier: "NewViewController") {
present(viewController, animated: true, completion: nil)
}