Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift &引用;“索引超出范围”;一个Viewcontroller中有两个TableView时出错_Swift_Uitableview_Return_Tableview - Fatal编程技术网

Swift &引用;“索引超出范围”;一个Viewcontroller中有两个TableView时出错

Swift &引用;“索引超出范围”;一个Viewcontroller中有两个TableView时出错,swift,uitableview,return,tableview,Swift,Uitableview,Return,Tableview,我在一个ViewController中有两个TableView。但是如果我运行我的代码,我将在第let post=posts[indexPath.row]行上得到这个错误: “索引超出范围” 我想可能是因为那些帖子。伯爵,但我想不出来。当我在没有第二个Tableview的情况下运行这段代码时,Tableview\u moremaps一切正常 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -

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

“索引超出范围”

我想可能是因为那些帖子。伯爵,但我想不出来。当我在没有第二个Tableview的情况下运行这段代码时,
Tableview\u 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()
        }

我认为问题在于你使用cellForRowAt的方式

尝试使用
tableview.accessibilityIdentifier=“myIdentifier”
然后,
cellForRowAt
将如下所示:

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

            if tableView.accessibilityIdentifier == "myIdentifier" {
                let cell = .....
                ...
...
...
...
...

numberofrowsin部分
中,如果条件

   if tableView === self.tableView {
        return comments.count
    } else {
        return posts.count
    }
当前
tableView==tableView
始终为
true
,返回值为
comments
count。

您可以尝试此方法

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()
        }

如果我使用您的代码,当我尝试重新加载tableview时,它将给我另一个错误:“线程1:EXC_BAD_ACCESS(代码=2,地址=0x16d477fd0)”,因此,您有两个问题。你也必须解决这个问题。