将 UITableView 实例化为 popover 时,UITableViewCell 的属性为零 [英] UITableViewCell's attributes are nil when instantiating UITableView as a popover

查看:24
本文介绍了将 UITableView 实例化为 popover 时,UITableViewCell 的属性为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UISegmentedControlUIViewController,当我点击 UITableViewControllercode>segmentedControl 段.我遇到的问题是当我单击一个段时,弹出窗口开始加载,但在 myPopoverTableViewController 加载时崩溃.它在

I have a UIViewController with a UISegmentedControl from which I'd like to present a UITableViewController as a popover when I click on a segmentedControl segment. The issue I'm having is the moment I click on a segment, the popover starts to load, but crashes as myPopoverTableViewController loads. It crashes in

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

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

For simplicity's sake, I'll reference my classes here:

myViewController: MyViewController
myPopoverTVController: PopoverTableViewController
myPopoverTVCell: PopoverTableViewCell

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

In lldb, I checked for the values of the cell, the dataSource, and it seems the only thing that's nil are the the attributes of myPopoverTVCell, which I register in myPopoverTVController's viewWillAppear with the following line:

tableView.register(PopoverTableViewCell.self, forCellReuseIdentifier: "cell")

myPopoverTVController 没有通过 popover segue(虽然我已经尝试过)连接到 myViewController.我已经检查过我在 myPopoverTVController's 原型单元的类中引用了 PopoverTableViewCell.我已经仔细检查了从单元格到 PopoverTableViewCell 类的连接.我检查了故事板上的表视图单元格的标识符是否设置为 cell.

myPopoverTVController is not connected via a popover segue (though I've tried that) to myViewController. I've checked that I have PopoverTableViewCell referenced in the class for myPopoverTVController's prototype cell. I've double checked the connections from the cell to to the PopoverTableViewCell class. I checked that I've got the Table View Cell's identifier set to cell on the storyboard.

以下是我如何从 myViewController 开始弹出窗口,遵循 Apple 的代码:

Here's how I begin the popover from myViewController, following Apple's 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
}

myPopoverTVController 上,这是我的 cellForRowAt indexPath 的样子:

On myPopoverTVController, here's what my cellForRowAt indexPath looks like:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PopoverTableViewCell
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PopoverTableViewCell

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

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

        cell.areaLabel?.text = countryList[indexPath.row]
        cell.flagLabel?.text = countryCodes[indexPath.row]

    default:
        break
    }

    return cell
}

我检查了在 myViewController 上实例化时设置的变量,它们都有值.它只是 tableViewCell 属性是 nil--lldb 当我输入 po cell 返回单元格的内存地址>.我已经设置了 UITableViews 一百万次,但我无法弄清楚这件事.任何建议:非常感谢我做错了什么.我可以放心,我的问题是我的一个非常愚蠢的遗漏.感谢您的阅读.

I checked the variables that are set upon instantiation on myViewController and they've all got values. It's just the tableViewCell attributes that are nil--lldb returns a memory address for the cell when I type po cell. I've set up UITableViews a million times, but I can't figure this thing out. Any suggestions re: what I'm doing wrong are greatly appreciated. I'll rest assured my problem is a startlingly silly omission on my part. Thank you for reading.

推荐答案

我在尝试使用 Apple 的代码时放弃了,并想出了一种替代方法来让 popover 工作.

I threw in the towel trying to use Apple's code and figured out an alternative method to get the popover to work.

我在情节提要中ctrl+dragged 从我的 myViewControllermyPopoverTVController.我将 segue 标识符 设置为 popoverSegue 并将其设置为显示为弹出框.我还指定了一个锚点.

I ctrl+dragged in storyboard from my myViewController to myPopoverTVController. I set the segue identifier to popoverSegue and set it to display as a popover. I also specified an anchor point.

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

From there, I gutted the code in segmentedControlAction() to replaced it with the following:

@IBAction func segmentedControlAction(_ sender: UISegmentedControl) {

    self.performSegue(withIdentifier: "popoverSegue", sender: segmentedControl.selectedSegmentIndex)

}

我在 myViewController 上的 prepareForSegue 中添加了以下代码:

I added the following code to my prepareForSegue on myViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "popoverSegue" {
        let destinationViewController = segue.destination as! PopoverTableViewController
        destinationViewController.selectedSegmentIndex = sender as! Int
        destinationViewController.currentRegion = currentRegion


        let popoverController = destinationViewController.popoverPresentationController

        if popoverController != nil {
            popoverController?.delegate = self
        }
    }
}

我还向 myViewController 添加了一个带有扩展名的委托方法:

I also added a delegate method to myViewController with an extension:

extension MyViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

最后,我在 myPopoverTVController 中取出了对数据源的本地引用,现在看起来像这样:

Lastly, I took out a local reference to the data source in myPopoverTVController so it now looks like this:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PopoverTableViewCell

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

    case  1:
        cell.areaLabel.text = locationManager.stateList(geographicRegion: currentRegion!)[indexPath.row]
        cell.flagLabel.isHidden = true

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

    default:
        break
    }

    return cell
}

...它奏效了.

结束;)

这篇关于将 UITableView 实例化为 popover 时,UITableViewCell 的属性为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆