Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 分组表视图发送nil,但Plain工作正常_Ios_Arrays_Swift_Uitableview_Segue - Fatal编程技术网

Ios 分组表视图发送nil,但Plain工作正常

Ios 分组表视图发送nil,但Plain工作正常,ios,arrays,swift,uitableview,segue,Ios,Arrays,Swift,Uitableview,Segue,当我对我的表进行分组时,它会发送nil,但当我将所有内容保持为1数组时,我没有问题,也看不出我做错了什么?下面是我发送nil的分组表视图的代码,我希望这是一个简单的修复。谢谢 视图控制器: import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var

当我对我的表进行分组时,它会发送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.

    }

}
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
    }

}
第二视图控制器:

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.

    }

}
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
。相反,将项的索引另存为变量并从中读取。由于发送者可以被强制转换为任何对象,所以将来维护您的项目将更加困难

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

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
    }
}

您可以尝试避免在
prepareForSegue
功能中使用
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
    }
}

您是否将情节提要中的segue标识符设置为“segue”?什么发送了
nil
?从哪里寄来的?您的问题完全不清楚。抱歉@rmaddy是的,我在回读我的问题时含糊不清:)我的表在我从数组中设置的组中完全填充,但当我单击单元格时,它应该转到SecondViewController并显示数组中存储的所有信息,但它返回零。我希望这能解释得更清楚一点。对不起,您是否将情节提要中的segue标识符设置为“segue”?什么发送了
nil
?从哪里寄来的?您的问题完全不清楚。抱歉@rmaddy是的,我在回读我的问题时含糊不清:)我的表在我从数组中设置的组中完全填充,但当我单击单元格时,它应该转到SecondViewController并显示数组中存储的所有信息,但它返回零。我希望这能解释得更清楚一点。很抱歉,感谢您的回复,通过遵循您的代码,我现在得到一个错误,无法将“[Rune]”类型的值分配给“Rune!”有什么办法可以解决这个问题吗?对不起,我忘了在这里标记你:)@JohnDoe你是在哪段代码上得到这个错误的?谢谢你的回复,我在这里得到它destinationVC.selectedRune=rune@JohnDoe好的,你的
符文
是一个数组,在每个子数组中,只有一个
符文
对象。你打算这样做吗?在我更新我的回答之前,请确认一下谢谢你的回复,通过遵循你的代码,我现在得到一个错误,无法将“[Rune]”类型的值赋给“Rune!”有什么办法可以解决这个问题吗?对不起,我忘了在这里标记你:)@JohnDoe你是在哪段代码上得到这个错误的?谢谢你的回复,我在这里得到它destinationVC.selectedRune=rune@JohnDoe好的,你的
符文
是一个数组,在每个子数组中,只有一个
符文
对象。你打算这样做吗?在我更新答案之前先确认一下