【问题标题】:How to determine if UIView has loaded [duplicate]如何确定 UIView 是否已加载 [重复]
【发布时间】:2017-07-31 07:00:44
【问题描述】:

我目前正在创建自定义视图。这个视图有一个UITableView。此控件的委托将是自定义控件的支持类。

class MyView: UIView {

    @IBOutlet weak var autoCompleteView: UITableView!

}

extension MyView: UITableViewDelegate {

}

extension MyView: UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath as IndexPath)
        cell.textLabel!.text =  "\(indexPath.row) - Its working"
        return cell
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
}

现在,在这个类中,我需要获取UITableView 的委托,但这只能在视图加载后发生(否则autoCompleteView 将是nil)。

如果它在UIViewController 中,我可以简单地将它添加到viewDidLoad,但我没有在这里打开它。那么我该如何设置UITableView的委托

【问题讨论】:

    标签: ios swift uitableview uiview


    【解决方案1】:

    现在,在这个类中,我需要获取 UITableView 的委托,但这只能在加载视图后发生

    我不完全确定你的意思。视图MyView 被加载,如果你在它的一个方法中。如果该类不存在,则不能调用它的方法。因此,如果您在initWithFrame: 中进行配置,一切都应该没问题。

    否则我唯一能想到的就是如果您的视图在 Interface Builder 中布局并因此将被解码。在这种情况下,虽然视图可能已经加载,但某些引用可能尚未加载。为此,您将在所有解码完成后接到awakeFromNib 的电话。

    【讨论】:

      【解决方案2】:

      //你可以试试这些

      class MyView: UIView {
          @IBOutlet weak var autoCompleteView: UITableView!
      
          override func willMove(toSuperview newSuperview: UIView?) {
              super.willMove(toSuperview: newSuperview)
          }
      
          override func didMoveToSuperview() {
              super.didMoveToSuperview()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 2010-09-20
        • 1970-01-01
        • 2010-09-20
        相关资源
        最近更新 更多