0

I have an app that has tableview. The content of tableView is like twitter/instagram is fetch from EndPoint A,for example. While there are button follow/unfollow that need to fetch the status from Endpoint B.

I already tried to call the EndPoint B from every cell but i think because the asynchronous, the status is shown on random cell.

How do i get to fetch the status that has different endpoint while keep my apps smooth?

Here's the code that i've used:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let checkStatus = self.getFollowStatus(userID: id)
    if (checkStatus) {
         cell.followButton.setTitle("Unfollow", for: UIControlState.normal)
    } else {
         cell.followButton.setTitle("Follow", for: UIControlState.normal)
    }
  //rest the code
}

  func getFollowStatus(userID:String) -> Bool {
    Alamofire.request(url, method: .get, parameters: nil, headers:headers)
        .responseJSON {response in
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                if let data = JSON["data"] as? [String: Any]{
                    let status = data["follow_status"] as! String
                    if status == "follow" {
                        self.isFollow = true
                    } else {
                         self.isFollow = false
                    }
                }
            }
    return self.isFollow
}

Thanks..

user2373192
  • 31
  • 1
  • 5
  • You can assign a tag to the button in a particular cell in order to get the status for the cell and on receiving the response you can reload the the table view to reflect the changes. – Jitendra Solanki May 10 '17 at 10:53
  • See [this](http://stackoverflow.com/questions/43871746/view-freezes-with-multiple-dispatchqueue-main-async-use-in-swift-3#43872082) answer – Dávid Pásztor May 10 '17 at 11:00
  • 1
    `getFollowState` should take a completion closure and in that closure you can update the cell – Paulw11 May 10 '17 at 11:21
  • Hi @Paulw11 can u provide some snippet to handle the completion closure?i'm little noob in here...thanks – user2373192 May 10 '17 at 12:47

0 Answers0