分组表视图发送 nil 但普通工作完美

Posted

技术标签:

【中文标题】分组表视图发送 nil 但普通工作完美【英文标题】:Grouped Table View sends nil but Plain works perfectly 【发布时间】:2017-06-25 11:13:43 【问题描述】:

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

视图控制器:

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.

    


SecondViewController:

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
    


【问题讨论】:

您是否将情节提要中的 segue 标识符设置为“segue”? 发送什么nil?从哪里寄来的?您的问题完全不清楚。 对不起@rmaddy 是的,我在回读我的问题时含糊其辞存储在数组中的所有信息,但它返回 nil。我希望这能解释得更好一点。对不起 【参考方案1】:

您可以尽量避免在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
    

【讨论】:

感谢您的回复,按照您的代码我现在收到一个错误无法将类型“[Rune]”的值分配给类型“Rune!”有什么想法可以解决这个问题吗? 对不起,我忘了在这里标记你:) @JohnDoe 您在哪段代码中收到此错误? 感谢您的回复,我在这里找到了 destinationVC.selectedRune = rune @JohnDoe 好的,所以您的 runes 是一个数组数组,在每个子数组中,只有一个 Rune 对象。你打算这样做吗?只是在我更新我的答案之前确保

以上是关于分组表视图发送 nil 但普通工作完美的主要内容,如果未能解决你的问题,请参考以下文章

分组表视图空白部分

分组表视图中单元格的索引路径

如何删除分组表视图中标题和第一行之间的分隔符

分组表视图中的中间动画看起来很糟糕

IOS 分节表视图

在 prepareForSeque 时委托不是 nil,但在导航表视图中调用时为 nil