简体   繁体   中英

Sometimes getting the error, otherwise working fine , how to make sure to remove the error?

I am using the below code, in which I am using two cells, one is fixed other one is repeated in the tableview as per the data retrieved, some times it is working fine but some times it is giving error

Fatal error: Index out of range

at

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

How should I get rid of this error?

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

change the condition from

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

to

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

Your indexPath.row starts at 0 with the first cell. If in your application there's an instance where you get indexPath.row = 0 and scores1.count != 1 , you'll enter the else with an indexPath.row = 0

So, in that instance if you do posts[indexPath.row - 1] you're accessing an array with a negative index, which is causing the crash.

Try:

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

You also might want to remove the target before adding, as it might reuse previous target which will give you unwanted results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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