菜鸟教程小白 发表于 2022-12-11 18:20:23

ios - TableView 数组 segue 不会传递数据


                                            <p><p>我在将数据从 TableView 中的选定行传递到另一个 ViewController 时遇到问题。</p>

<pre><code>class SectionsTableViewController: UITableViewController {


var sections: = SectionsData().getSectionsFromData()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    // #warning Incomplete implementation, return the number of rows
    return sections.items.count
}


override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
    return sections.headings
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;sectionCell&#34;, for: indexPath)

    // Configure the cell...
    cell.textLabel?.text = sections.items

    return cell
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if (segue.identifier == &#34;sectionCell&#34;)
    {
      let upcoming: Sjekkliste = segue.destination as! Sjekkliste

      let indexPath = self.tableView.indexPathForSelectedRow!

      let titleString = Sections(title: &#34;&#34;, objects: [&#34;&#34;]) as? String

      upcoming.titleString = titleString

      self.tableView.deselectRow(at: indexPath, animated: true)
      }
</code></pre>

<p>这是我的问题所在:<code>let titleString = Sections(title: "", objects: [""]) as?字符串</code></p>

<p>如果标题和对象分开传递会更好。</p>

<p>这是我的数据设置:</p>

<pre><code>class SectionsData {
var myArray: = []

func getSectionsFromData() -&gt; {

    var sectionsArray = ()

    let Generell = Sections(title: &#34;Animals&#34;, objects:
      [&#34;Cat&#34;, &#34;Dog&#34;, &#34;Lion&#34;, &#34;Tiger&#34;])
    sectionsArray.append(Generell)
    return sectionsArray
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这一行建议您将 String 子类化为 Sections。你为什么要继承字符串?如果 Sections 不是字符串的子类,则此行将失败。</p>

<pre><code>let titleString = Sections(title: &#34;&#34;, objects: [&#34;&#34;]) as? String
</code></pre>

<p>我假设你想要类似的东西:</p>

<pre><code>let selectedSection:Sections = Sections(title: &#34;&#34;, objects: [&#34;&#34;])
upcoming.titleString = selectedSection.title
</code></pre>

<p>上面建议 Sections 对象上的“title”属性是您希望设置为下一个 ViewController 的“titleString”属性的内容。 </p>

<p>与原始问题无关,您应该将“Generell”小写。最佳实践建议只有类名应该以大写字母开头。我还建议实现 didSelectRow 方法来选择您的数据。结果应该类似于:</p>

<pre><code>var selectedSection:Sections? //goes at top

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    self.selectedSection = self.sections
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == &#34;sectionCell&#34;){
    let upcoming: Sjekkliste = segue.destination as! Sjekkliste
    if let section = self.selectedSection{
      upcoming.titleString = section.title
    }
}
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - TableView 数组 segue 不会传递数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41190455/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41190455/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - TableView 数组 segue 不会传递数据