【问题标题】:"Index out of range" Error with two Tableviews in one Viewcontroller一个 Viewcontroller 中有两个 Tableview 的“索引超出范围”错误
【发布时间】:2019-06-11 09:44:17
【问题描述】:

我在一个 ViewController 中有两个 Tableview。但是,如果我运行我的代码,我将在let post = posts[indexPath.row] 行收到此错误:

“索引超出范围”

我认为它与posts.count 有关,但我无法弄清楚。当我在没有第二个 Tableview Tableview_moremaps 的情况下运行此代码时,一切正常。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if comments.count == 0  {
                self.tableView.setEmptyMessage("No comments yet!")
            } else {
                self.tableView.restore()
            }

            if posts.count == 0 {
                self.Tableview_moremaps.setEmptyMessage("No other maps yet!")
            }else{
                self.Tableview_moremaps.restore()
            }

            if tableView == tableView {
                return comments.count
            }else{
                return posts.count
            }

        }

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

            if tableView == self.tableView {
                let cell = tableView.dequeueReusableCell(withIdentifier: "Comment", for: indexPath) as? CommentTableViewCell
                let comment = comments[indexPath.row]
                cell?.comment = comment
                cell?.delegate = self

                return cell!
            } else if tableView == Tableview_moremaps {
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? MoremapsTableViewCell
                cell?.Map_image.image = nil
                let post = posts[indexPath.row]
                cell?.post = post
                cell?.delegate = self
                return cell!
            }

            return UITableViewCell()
        }

【问题讨论】:

    标签: swift uitableview return tableview


    【解决方案1】:

    numberOfRowsInSection 中,更新if-condition

       if tableView === self.tableView {
            return comments.count
        } else {
            return posts.count
        }
    

    目前tableView == tableView 始终为true,返回值为comments 计数。

    【讨论】:

    • 如果我使用你的代码,当我尝试重新加载我的 tableview 时,它会给我另一个错误:“线程 1:EXC_BAD_ACCESS (code=2, address=0x16d477fd0)”
    • 所以,你有两个问题。你也必须解决这个问题。
    【解决方案2】:

    我认为问题出在您使用cellForRowAt的方式上

    尝试使用tableView.accessibilityIdentifier = "myIdentifier" 为每个表视图分配一个标识符 然后cellForRowAt会像下面这样:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
                if tableView.accessibilityIdentifier == "myIdentifier" {
                    let cell = .....
                    ...
    ...
    ...
    ...
    ...
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                 if tableView == self.tableView {
              return comments.count
      
          }
          else{
              return posts.count
          }
      }
      
      //Your this code is perfect.(no issue here)
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
                  if tableView == self.tableView {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "Comment", for: indexPath) as? CommentTableViewCell
                      let comment = comments[indexPath.row]
                      cell?.comment = comment
                      cell?.delegate = self
      
                      return cell!
                  } else if tableView == Tableview_moremaps {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? MoremapsTableViewCell
                      cell?.Map_image.image = nil
                      let post = posts[indexPath.row]
                      cell?.post = post
                      cell?.delegate = self
                      return cell!
                  }
      
                  return UITableViewCell()
              }
      

      【讨论】:

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