2 years ago

#67296

test-img

D. A.

TableView is not updating after deleting

I'm using dummy API to create a social media app. I managed to show posts details in a table view and a user can reply to a post and delete it. However, when I delete a post it gets deleted but the table view is not updated. I have to sign out then in for it to be updated. I have tried to reload the date in several place but to no avail!

I created a del button in PostCell then made it send a notification with the postId to be received in the post viewcontroller where the tableView is defined then created a function that does the deletion process and reload the data in the same viewCotroller.

Any help will be most appreciated.

Here is the code:

@objc func postDeleted(notification: Notification){
    if let cell = notification.userInfo?["cell"] as? UITableViewCell{
        if let indexPath = postsTableView.indexPath(for: cell){
            let post = posts[indexPath.row]
            postId = post.id
            if UserManager.signedInUser?.firstName == post.owner.firstName && UserManager.signedInUser?.lastName == post.owner.lastName{
                    PostAPI.deletePost(postId: self.postId) { postIdResponse in
                        PostAPI.getAllPosts(page: page, tag: tag) { postResponse,total in
                            self.posts.append(contentsOf: postResponse)
                            self.total = total
                            self.postsTableView.reloadData()
                        }
                }
            }
        }
    }
}

swift

uitableview

0 Answers

Your Answer

Accepted video resources