菜鸟教程小白 发表于 2022-12-11 18:13:38

ios - 将 UITableView 实例化为弹出框时,UITableViewCell 的属性为零


                                            <p><p>我有一个带有 <code>UISegmentedControl</code> 的 <code>UIViewController</code>,当我点击 <code>UITableViewController</code> code>segmentedControl</code> 段。我遇到的问题是当我点击一个片段时,弹出框开始加载,但在加载 myPopoverTableViewController 时崩溃。它崩溃了</p>

<p><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell</code>,说我的<code>PopoverTableViewCell</code>的属性是<code>nil</code> .</p>

<p>为简单起见,我将在此处引用我的类:</p>

<pre><code>myViewController: MyViewController
myPopoverTVController: PopoverTableViewController
myPopoverTVCell: PopoverTableViewCell
</code></pre>

<p>在 <code>lldb</code> 中,我检查了单元格的值 <code>dataSource</code>,似乎唯一为零的是 <code>myPopoverTVCell</code>,我在 <code>myPopoverTVController 的 viewWillAppear</code> 中注册了以下行:</p>

<pre><code>tableView.register(PopoverTableViewCell.self, forCellReuseIdentifier: &#34;cell&#34;)
</code></pre>

<p><code>myPopoverTVController</code> 没有通过 popover segue(尽管我已经尝试过)连接到 <code>myViewController</code>。我检查了 <code>myPopoverTVController's</code> 原型(prototype)单元的类中是否引用了 <code>PopoverTableViewCell</code>。我已经仔细检查了从单元到 <code>PopoverTableViewCell</code> 类的连接。我检查了我已在 Storyboard 上将 Table View Cell 的标识符设置为 <code>cell</code>。</p>

<p>下面是我如何从 <code>myViewController</code> 开始弹出窗口,跟在 <a href="https://developer.apple.com/reference/uikit/uipopoverpresentationcontroller" rel="noreferrer noopener nofollow">Apple&#39;s code</a> 之后:</p>

<pre><code>@IBAction func segmentedControlAction(_ sender: UISegmentedControl) {
    // instantiate the PopoverTableViewController
    let popoverTVC = PopoverTableViewController()
    // set variables on it
    popoverTVC.selectedSegmentIndex = sender.selectedSegmentIndex
    popoverTVC.currentRegion = currentRegion
    // disignate presentation style as a popover
    popoverTVC.modalPresentationStyle = .popover

    present(popoverTVC, animated: true, completion: nil)

    let presentationController = UIPopoverPresentationController(presentedViewController: popoverTVC, presenting: self)
    presentationController.permittedArrowDirections = .up
    presentationController.sourceView = view
    presentationController.sourceRect = segmentedControl.frame
}
</code></pre>

<p>在 <code>myPopoverTVController</code> 上,这是我的 <code>cellForRowAt indexPath</code> 的样子:</p>

<pre><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    // let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;, for: indexPath) as! PopoverTableViewCell
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;) as! PopoverTableViewCell

    // Configure the cell...
    switch selectedSegmentIndex {
    case 0, 1:
      cell.areaLabel.text = popoverStringArray

    case 2:
      let countryList = locationManager.countryList
      let countryCodes = locationManager.countryCodes

      cell.areaLabel?.text = countryList
      cell.flagLabel?.text = countryCodes

    default:
      break
    }

    return cell
}
</code></pre>

<p>我检查了在 <code>myViewController</code> 上实例化时设置的变量,它们都有值。只是 <code>tableViewCell</code> 属性是 <code>nil</code>--当我键入 <code>po cell</code> 时,<code>lldb</code> 返回单元格的内存地址>。我已经设置了 <code>UITableViews</code> 一百万次,但我无法弄清楚这件事。任何建议:非常感谢我做错了什么。我会放心,我的问题是我的一个令人吃惊的愚蠢遗漏。感谢您的阅读。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我放弃了尝试使用 Apple 的代码,并想出了一种替代方法来让弹出框工作。 </p>

<p>我在 Storyboard 中 <code>ctrl+dragged</code> 从我的 <code>myViewController</code> 到 <code>myPopoverTVController</code>。我将 <strong>segue identifier</strong> 设置为 <code>popoverSegue</code> 并将其设置为显示为弹出框。我还指定了一个 anchor 。</p>

<p>从那里,我删除了 <code>segmentedControlAction()</code> 中的代码,将其替换为以下内容:</p>

<pre><code>@IBAction func segmentedControlAction(_ sender: UISegmentedControl) {

    self.performSegue(withIdentifier: &#34;popoverSegue&#34;, sender: segmentedControl.selectedSegmentIndex)

}
</code></pre>

<p>我在 <code>myViewController</code> 上的 <code>prepareForSegue</code> 中添加了以下代码:</p>

<pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == &#34;popoverSegue&#34; {
      let destinationViewController = segue.destination as! PopoverTableViewController
      destinationViewController.selectedSegmentIndex = sender as! Int
      destinationViewController.currentRegion = currentRegion


      let popoverController = destinationViewController.popoverPresentationController

      if popoverController != nil {
            popoverController?.delegate = self
      }
    }
}
</code></pre>

<p>我还向 <code>myViewController</code> 添加了一个委托(delegate)方法,并带有扩展名:</p>

<pre><code>extension MyViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -&gt; UIModalPresentationStyle {
      return .none
    }
}
</code></pre>

<p>最后,我在 <code>myPopoverTVController</code> 中取出了对数据源的本地引用,所以它现在看起来像这样:</p>

<pre><code>override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;, for: indexPath) as! PopoverTableViewCell

    // Configure the cell...
    switch selectedSegmentIndex {
    case 0:
      cell.areaLabel.text = locationManager.cityList(geographicRegion: currentRegion!)
      cell.flagLabel.isHidden = true

    case1:
      cell.areaLabel.text = locationManager.stateList(geographicRegion: currentRegion!)
      cell.flagLabel.isHidden = true

    case 2:
      cell.areaLabel?.text = locationManager.countryList
      cell.flagLabel?.text = locationManager.countryCodes.flag()

    default:
      break
    }

    return cell
}
</code></pre>

<p>...它奏效了。</p>

<p>结束 ;)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 UITableView 实例化为弹出框时,UITableViewCell 的属性为零,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40918663/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40918663/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 UITableView 实例化为弹出框时,UITableViewCell 的属性为零