【问题标题】:UITableView throws exception when adding UIView to tableFooterView将 UIView 添加到 tableFooterView 时 UITableView 抛出异常
【发布时间】:2016-06-27 05:56:05
【问题描述】:

我正在尝试实现 UITableView。我已经能够让它工作了,但是它一直在空单元格中显示线条,所以我上网查找了一个解决方案,即在 tableFooterView 中放置一个 UIView,所以我做到了:

self.tableView.tableFooterView = IUView()

在我设置的第一个表视图中它运行良好,但我知道我试图实现第二个表视图并且它不起作用。如果我在没有上述行的情况下运行它,一切正常,但我在空单元格中得到了行。如果我使用上面的行运行,我会得到一个“致命错误:在展开可选值时意外发现 nil”,并且 XCode 在此行中指出错误:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int)     -> Int {
        return self.itemsList.count // Throws error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
}

有谁知道为什么会这样?或者我该如何解决?或者,如果有更好、更正确的方法来隐藏空单元格上的行?谢谢!

这里是我的 UIViewController 类供参考:

class InfoPickerViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var infoPicker: UITableView!

private var pageTitle: String!
private var itemsList: [String]!

convenience init(title: String){
    self.init()

    self.pageTitle = title
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    self.title = self.pageTitle

    self.infoPicker.delegate = self
    self.infoPicker.dataSource = self
    self.infoPicker.tableFooterView = UIView()
    self.infoPicker.registerNib(UINib(nibName:"SimpleTableViewCell", bundle: nil), forCellReuseIdentifier: "simpleCell")

    populateItemsList()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func populateItemsList() {
    let clientsInfo = ClientsInfo()
    self.itemsList = []

    switch self.pageTitle {
        case "Cliente": self.itemsList = clientsInfo.getClientsList()
        default: self.itemsList = []
    }
}

// MARK: TableView

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.itemsList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let listItemCell = infoPicker.dequeueReusableCellWithIdentifier("simpleCell", forIndexPath: indexPath) as! SimpleTableViewCel

    listItemCell.itemLabel.text = self.itemsList[indexPath.row]

    return listItemCell
}

}

【问题讨论】:

  • 好的,我设法让它直接在 .xib 文件中添加 UIView,而不是以编程方式。不过,我想知道错误来自哪里。
  • 我在回答中描述了错误的来源。您有一个未初始化的itemsList: [String]!,当您的numberOfRowsInSection 尝试访问该值时,该值为零并且您的应用程序崩溃。我的解决方案通过给你的数组一个初始值来解决这个问题。

标签: ios iphone swift uitableview uiview


【解决方案1】:

我没有带我的 mac,但我会试一试。在ViewController 的顶部,更新以下行:

private var itemsList: [String] = [] {
    didSet {
        dispatch_async(dispatch_get_main_queue(), { 
            self.tableView.reloadData()
        })
    }
}

我们正在初始化您的itemsList 数组,该数组的元素计数为0,这样您的numberOfRowsInSection 就不会崩溃,因为您的itemsListnil。然后,当你的populateItemsList函数给itemsList一个真正的值时,你的表视图将在主线程上重新加载数据。


来自 cmets:

我在回答中描述了错误的来源。您有一个未初始化的itemsList: [String]!,当您的numberOfRowsInSection 尝试访问该值时,该值为零并且您的应用程序崩溃。我的解决方案通过给你的数组一个初始值来解决这个问题。

【讨论】:

  • 我总是犯这个错误。谢谢
猜你喜欢
  • 2012-03-05
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2013-05-24
  • 2010-11-24
  • 2012-04-01
  • 2015-07-19
相关资源
最近更新 更多