jjzjj

ios - 将 UITableView 实例化为弹出窗口时,UITableViewCell 的属性为 nil

coder 2024-01-29 原文

我有一个 UIViewController 和一个 UISegmentedControl,当我点击一个 segmentedControl 段。我遇到的问题是当我单击一个段时,弹出窗口开始加载,但随着 myPopoverTableViewController 加载而崩溃。它崩溃了

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

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

myViewController: MyViewController
myPopoverTVController: PopoverTableViewController
myPopoverTVCell: PopoverTableViewCell

lldb 中,我检查了单元格的值,dataSource,似乎唯一没有的是 myPopoverTVCell<>,我使用以下行在 myPopoverTVController's viewWillAppear 中注册:

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

myPopoverTVController 没有通过 popover segue(虽然我已经试过了)连接到 myViewController。我检查过我在类中为 myPopoverTVController 的 原型(prototype)单元格引用了 PopoverTableViewCell。我仔细检查了从单元格到 PopoverTableViewCell 类的连接。我检查了 Storyboard上的 TableView 单元格的标识符是否设置为 cell

下面是我如何从 myViewController 开始弹出窗口,在 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 的样子:

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——当我键入 po cell 时,lldb 返回单元格的内存地址>。我已经设置了 UITableViews 一百万次,但我无法弄清楚这件事。非常感谢任何关于我做错了什么的建议。我会放心,我的问题是我的一个非常愚蠢的疏忽。感谢您阅读。

最佳答案

我放弃了尝试使用 Apple 代码的尝试,并找到了一种替代方法来让弹出窗口正常工作。

我在 Storyboard 中 ctrl+dragged 从我的 myViewControllermyPopoverTVController。我将 segue 标识符 设置为 popoverSegue 并将其设置为显示为弹出窗口。我还指定了一个 anchor 。

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

@IBAction func segmentedControlAction(_ sender: UISegmentedControl) {

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

}

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

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 添加了一个带有扩展名的委托(delegate)方法:

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

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

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
}

...它成功了。

结束;)

关于ios - 将 UITableView 实例化为弹出窗口时,UITableViewCell 的属性为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918663/

有关ios - 将 UITableView 实例化为弹出窗口时,UITableViewCell 的属性为 nil的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  3. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  4. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  5. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  6. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  9. ruby-on-rails - Rails 中的 NoMethodError::MailersController#preview undefined method `activation_token=' for nil:NilClass - 2

    似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai

  10. ruby-on-rails - Rails 模型——非持久类成员或属性? - 2

    对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs

随机推荐