gpt4 book ai didi

ios - Grouped Table View 发送 nil 但 Plain 完美运行

转载 作者:行者123 更新时间:2023-11-28 08:07:20 25 4
gpt4 key购买 nike

我在对表进行分组时遇到问题,它发送 nil 但是当我将所有内容都保留为 1 个数组时,我没有问题并且看不到我做错了什么?这是我发送 nil 的分组 TableView 的代码,我希望这是一个简单的修复。谢谢

View Controller :

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var runeTitle = [["Freyr/Freya's Aett"], ["Heimdall's Aett"], ["Tyr's Aett"], ["Additional Runes"]]

let runes = [[Rune(runeName: "Fehu", runeImage: UIImage(named: ("Fehu.png"))!, runeDescription: "(F: Domestic cattle, wealth.) Possessions won or earned, earned income, luck. Abundance, financial strength in the present or near future. Sign of hope and plenty, success and happiness. Social success. Energy, foresight, fertility, creation/destruction (becoming).\n\n Fehu Reversed or Merkstave: Loss of personal property, esteem, or something that you put in effort to keep. It indicates some sort of failure. Greed, burnout, atrophy, discord. Cowardice, stupidity, dullness, poverty, slavery, bondage.")],

[Rune(runeName: "Hagalaz", runeImage: UIImage(named: ("Hagalaz.png"))!, runeDescription: "(H: Hail.) Wrath of nature, destructive, uncontrolled forces, especially the weather, or within the unconscious. Tempering, testing, trial. Controlled crisis, leading to completion, inner harmony. Hagalaz Merkstave (Hagalaz cannot be reversed, but may lie in opposition): Natural disaster, catastrophe. Stagnation, loss of power. Pain, loss, suffering, hardship, sickness, crisis.")],

[Rune(runeName: "Blank Rune", runeImage: UIImage(named: ("Blank.png"))!, runeDescription: "There is no historical support for a \"Blank Rune\" in runic divination. It was invented in the 1980's. It should not be used in a rune casting. If you bought a rune set with a blank piece, save it in case you lose another rune piece, but don't use it in rune casting.")]]

func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return runes.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return runes[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// Configure the cell...

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell;

let item = runes[indexPath.section][indexPath.row]
cell.imageView?.image = item.runeImage
cell.textLabel?.text = item.runeName
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator

return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return String(describing: runeTitle[section][0])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {

let rune = sender as! Rune
let destinationVC = segue.destination as! SecondViewController
destinationVC.selectedRune = rune
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

}

第二 View Controller :

import UIKit

class SecondViewController: UIViewController {

var selectedRune: Rune!

@IBOutlet weak var runeNameLabel: UILabel!
@IBOutlet weak var runeImage: UIImageView!
@IBOutlet weak var runeDescriptionLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

runeNameLabel.text = selectedRune.runeName
runeImage.image = selectedRune.runeImage
runeDescriptionLabel.text = selectedRune.runeDescription
}

}

最佳答案

你可以尽量避免在prepareForSegue函数中使用sender。相反,将项目的索引保存为变量并从中读取。由于 sender 可以转换为任何东西,因此将来维护您的项目将更加困难。

所以你正在寻找这样的东西:

var clickedIndex = 0

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.clickedIndex = indexPath.row
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {

let runeArray = runes[clickedIndex]// variable runeArray is the child array from runes
let destinationVC = segue.destination as! SecondViewController
destinationVC.selectedRune = runeArray[0]// Now choose an actual Rune object from the array by index
}
}

关于ios - Grouped Table View 发送 nil 但 Plain 完美运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com