0

I have a tableview with at all times one cell. If there is data to download from Firebase it will put it in an array called posts. When there's for example, two "in my case" servers that the user will download, it will only display one cell instead of two. I thought I could fix this by changing return posts.count to return posts.count + 1 because of the one cell that will be shown at all times. But if I use return posts.count + 1 I will get a

Thread 1: Fatal error: Index out of range

error on line let post = posts[indexPath.row]. I have read about this error, but I can't seem to fix it.

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if posts.count == 0 {
            self.tableView.setEmptyMessage("No Servers uploaded!")
            return 1
        } else {
            self.tableView.restore()
            return posts.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCellTableViewCell

            cell.delegate = self
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCellMYposts

            cell.Map_image.image = nil
            let post = posts[indexPath.row]
            cell.post = post
            cell.delegate = self

            return cell
        }
    }
1
  • Instead of mixing with the count property, add an entry to your posts array with the content "No Servers uploaded!" when the firebase request has returned an empty result set. That way you don't need any hackish solutions Commented Jun 13, 2019 at 20:28

1 Answer 1

1

Assuming you have some piece of data in posts[0], you are never actually displaying it. For indexPath.row = 0, you are displaying a profile cell, and then you start displaying the data from posts[1] and on. Change your problem line to: let post = posts[indexPath.row - 1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.