【问题标题】:tableView is nil when calling function from XIB从 XIB 调用函数时 tableView 为零
【发布时间】:2014-11-08 13:46:18
【问题描述】:

我有一个工作正常的 tableView。内容已正确填充。我在导航栏中也有几个按钮来过滤 tableView 和 reloadData 的内容。一切都很好。

但现在我正在尝试使用自定义表格视图标题,以便在其中放置一些按钮。我创建了一个 XIB 文件并将其添加到 tableview 标题中。

MainViewController.swft

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {       
    var headerxib:UIView = NSBundle.mainBundle().loadNibNamed("headerView", owner: self, options: nil).first as UIView        
    var tableheader = UIView(frame: CGRectMake(0, 0, 300, 80)) 
    tableHeader.addSubview(headerxib)
    return tableHeader        
}

// This is the function that I'm trying to run
func justReload(){
    println("reload \(tableView)")            
    tableView.reloadData()
}

到目前为止一切都很好。然后我在我的 XIB swift 文件中创建了 IBAction 以从主视图控制器(这是带有 tableview 的那个)调用一个函数

headerView.xib

@IBAction func touhcbb(sender: AnyObject) {
   let vc = MainViewController()        
   vc.justReload()               
}

所以,这就是问题所在。 justReload() 函数中的 println() 显示 tableView 为 nil。导致 reloadData() 出错。

如果函数是从外部 XIB 文件调用的,为什么它是 nil?

当我尝试从同一 ViewController 中的 IBOutlet 调用该函数时,一切正常。

如果有人有任何提示或线索可以为我指明正确的方向,我将不胜感激。谢谢!

【问题讨论】:

  • 试试 self.tableView.reloadData()
  • @derdida 也尝试过。还是没有
  • 你是在你的动作函数中创建一个新的 MainViewController 吗?那不会是具有表格视图的视图控制器。
  • @Swipesight 你能解释更多吗?从 XIB swift 文件调用 MainViewController 中现有函数的正确方法是什么? (我的tableView在MainViewController中)
  • @Swipesight erm 我不能这样做,因为该操作方法在另一个文件中。我更新了我的问题以更好地解释它。但无论如何,如果我在action方法中println(vc.tableView),它也是nil。

标签: ios uitableview swift xib


【解决方案1】:

打开您的 xib,并将文件所有者的类设置为 MainViewController:

MainViewContrtoller.swift

class MainViewController: UITableViewController {
    // Use IB to connect the button to this action method.
    @IBAction func touhcbb(sender: AnyObject) {
        justReload()
    }

    func justReload(){
        println("reload \(tableView)")            
        tableView.reloadData()
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerxib:UIView = NSBundle.mainBundle().loadNibNamed("headerView", owner: self, options: nil).first as UIView

        var tableheader = UIView(frame: CGRectMake(0, 0, 300, 80)) 
        tableHeader.addSubview(headerxib)

        return tableHeader
    }
}

【讨论】:

    【解决方案2】:

    我设法解决了这个问题。

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {              
        var headerxib: tableHeaderView = NSBundle.mainBundle().loadNibNamed("headerView", owner: self, options: nil).first as tableHeaderView   
    
        //Need this to send reference of MainViewController to the XIB's vc
        headerxib.vc = self     
    
        var tableheader = UIView(frame: CGRectMake(0, 0, 300, 80)) 
        tableHeader.addSubview(headerxib)
        return tableHeader        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多