简体   繁体   English

一个视图控制器中的 2 个表视图在 tableview.reloadData() 上出现错误

[英]2 tableviews in one viewController getting error on tableview.reloadData()

Hi I'm having an issue running 2 tableviews - one called 'tableView' and one called 'tableView1', both in same viewcontroller.嗨,我在运行 2 个表视图时遇到问题 - 一个名为“tableView”,一个名为“tableView1”,两者都在同一个视图控制器中。 code loads and runs but when I initiate tableView.reloadData() tableView1.reloadDate() I get an error (Unexpectedly found nil while unwrapping an optional value) If I comment out tableview1.reloadData() the first table runs and loads fine or vice versa.代码加载并运行,但是当我启动 tableView.reloadData() tableView1.reloadDate() 时出现错误(在展开可选值时意外发现 nil)如果我注释掉 tableview1.reloadData() 第一个表运行并加载正常,反之亦然反之亦然。 how do I reload both tables together.我如何一起重新加载两个表。 data is fetched from remote server via Json - offending code snippet below.数据是通过 Json 从远程服务器获取的 - 下面有问题的代码片段。


     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let countNumberRecordList = JSON_VDICheckFull.response?.dataItems?.numberRecordList?.count ?? 0

        if (countNumberRecordList > 0) {
            return countNumberRecordList
        } else {
            return 0
        }
    }

    
       func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(30.0)
    }
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellRow = indexPath.row


//************************************************//
                let cell = tableView.dequeueReusableCell(withIdentifier: "NumberRecordListCell") as! NumberRecordListCell

//**************Getting Error Here***************//

     
        
       cell.setNeedsLayout()
        cell.layoutIfNeeded()

  
       cell.fieldDate.text = JSON_VDICheckFull.response?.dataItems?.numberRecordList?[cellRow].dateOfInformation ?? "---"
        cell.fieldSource.text = JSON_VDICheckFull.response?.dataItems?.numberRecordList?[cellRow].sourceOfInformation ?? "---"
        cell.fieldNumber.text = String(JSON_VDICheckFull.response?.dataItems?.numberRecordList?[cellRow].number ?? 0)

        cell.separatorInset = UIEdgeInsets.zero
        cell.layoutMargins = UIEdgeInsets.zero

        cell.layoutIfNeeded()
        cell.layoutSubviews()

           return cell
    }
   

    func tableView1(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let countFinanceRecordList = JSON_VDICheckFull.response?.dataItems?.settingRecordList?.count ?? 0

        if (countSettingRecordList > 0) {
            return countSettingRecordList
        } else {
            return 0
        }
    }

   func tableView1(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(110.0)
    }

     func tableView1(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellRow = indexPath.row

         let cell = tableView.dequeueReusableCell(withIdentifier: "SettingRecordListCell") as! SettingRecordListCell

       cell.setNeedsLayout()
        cell.layoutIfNeeded()


   
        return cell

    }
    
       
    }

You should refer to the answers on this question.你应该参考这个问题的答案。

You should first use two different identifiers for your cells and check which table you are receiving in each tableView function.您应该首先为您的单元格使用两个不同的标识符,并检查您在每个 tableView function 中收到的表格。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == firstTableView,
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
        return cell
    } else if tableView == autoSuggestTableView,
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
        return cell
    }

    return UITableViewCell()
}

Link to question链接到问题

Trying to manage 2 table views in a single view controller is confusing.试图在一个视图 controller 中管理 2 个表视图是令人困惑的。 Typically, developers make the view controller serve as the data source (and delegate) for their (single) table view.通常,开发人员将视图 controller 用作其(单个)表视图的数据源(和委托)。

If you make your view controller the data source for both of your table views, all the data source methods will be called from both table views, and you have to include logic in all of those methods to say:如果您将视图 controller 作为两个表视图的数据源,则将从两个表视图调用所有数据源方法,并且您必须在所有这些方法中包含逻辑来说明:

tableView(_:numberOfRowsInSection:) method: tableView(_:numberOfRowsInSection:) 方法:

"If I'm being asked about table view 1, return the number of sections in that table view. If I'm being asked about table view 2, return the number of sections in that one." “如果我被问及表格视图 1,请返回该表格视图中的部分数量。如果我被问及表格视图 2,请返回该表格视图中的部分数量。”

tableView(_:cellForRowAt:) method: tableView(_:cellForRowAt:) 方法:

"If I'm being asked about table view 1, dequeue a cell using a cell identifier for table view 1, and configure that type of cell. If I'm being asked about table view 2, dequeue a cell using a cell identifier for table view 2, and configure that type of cell." “如果我被问到关于表视图 1 的问题,请使用表视图 1 的单元格标识符将一个单元格出列,并配置该类型的单元格。如果我被问到关于表视图 2 的问题,请使用单元格标识符将一个单元格出列表视图 2,并配置该类型的单元格。”


Your tableView(_:cellForRowAt:) method appears to be written to only dequeue and return one type of cell.您的tableView(_:cellForRowAt:)方法似乎只写入出列并返回一种类型的单元格。 It doesn't check to see which table view is asking for a cell.它不检查查看哪个表格视图正在请求一个单元格。 That's wrong.那是错误的。

It might be easier to set up separate objects to serve as the data source for each table view.设置单独的对象作为每个表视图的数据源可能更容易。 That way the data source methods don't have to have a bunch of "if table view 1, return data about that table, and if table view 2, return data about the other one" if/else statements.这样,数据源方法就不必有一堆“如果表视图 1,返回有关该表的数据,如果表视图 2,返回有关另一个表的数据”的 if/else 语句。 Each data source object can only worry about returning informaion about the table view it manages.每个数据源 object 只能担心返回有关它管理的表视图的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM