Ios 有时会出现错误,否则工作正常,如何确保消除错误?

Ios 有时会出现错误,否则工作正常,如何确保消除错误?,ios,swift,uitableview,Ios,Swift,Uitableview,我使用下面的代码,其中我使用了两个单元格,一个是固定的,另一个是根据检索到的数据在tableview中重复,有时工作正常,但有时出错 致命错误:索引超出范围 在 cell.set(post:posts[indexPath.row-1]) 我应该如何消除这个错误 override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableVi

我使用下面的代码,其中我使用了两个单元格,一个是固定的,另一个是根据检索到的数据在tableview中重复,有时工作正常,但有时出错

致命错误:索引超出范围

cell.set(post:posts[indexPath.row-1])

我应该如何消除这个错误

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   // print(posts)

    return scores1.count + posts.count

}

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

        if indexPath.row == 0 && scores1.count == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ScoresCell") as! ScoresCellInHomeScreen

            cell.set(scores: scores1[indexPath.row])
            return cell
        } else if posts.count > (indexPath.row - 1 ) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell

            cell.btnComment.tag = indexPath.row - 1
            cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)

            cell.favoritebutton.tag = indexPath.row - 1
            cell.favoritebutton.addTarget(self, action: #selector(favupdate(_:)), for: .touchUpInside)
            cell.set(post: posts[indexPath.row - 1 ])
            return cell
        } else {
            return UITableViewCell()
        }
    }

将条件从

else if posts.count > (indexPath.row - 1 )


您的
indexPath.row
从第一个单元格的0开始。 如果在您的应用程序中有一个实例,您可以获得
indexPath.row=0
scores1.count!=1
,您将使用
indepath.row=0

因此,在这种情况下,如果您发布[indexath.row-1],您正在访问一个索引为负的数组,这会导致崩溃。

请尝试:

if indexPath.row == 0 && scores1.count == 1 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ScoresCell") as! ScoresCellInHomeScreen

    cell.set(scores: scores1[indexPath.row])
    return cell
} else if indexPath.row > 0 {
    if posts.count > (indexPath.row - 1 ) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell

        cell.btnComment.tag = indexPath.row - 1
        cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)

        cell.favoritebutton.tag = indexPath.row - 1
        cell.favoritebutton.addTarget(self, action: #selector(favupdate(_:)), for: .touchUpInside)
        cell.set(post: posts[indexPath.row - 1 ])
        return cell
    } else {
        return UITableViewCell()
    }
}

您可能还想在添加之前删除该目标,因为它可能会重复使用以前的目标,这会给您带来不想要的结果。

您昨天问了这个问题,得到了一些回答,为什么要删除它?分数数组本地???
if indexPath.row == 0 && scores1.count == 1 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ScoresCell") as! ScoresCellInHomeScreen

    cell.set(scores: scores1[indexPath.row])
    return cell
} else if indexPath.row > 0 {
    if posts.count > (indexPath.row - 1 ) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell

        cell.btnComment.tag = indexPath.row - 1
        cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)

        cell.favoritebutton.tag = indexPath.row - 1
        cell.favoritebutton.addTarget(self, action: #selector(favupdate(_:)), for: .touchUpInside)
        cell.set(post: posts[indexPath.row - 1 ])
        return cell
    } else {
        return UITableViewCell()
    }
}