ios - how to update like button for every post in table view cell on server -


i trying make app instagram. have parsed json in newsfeed table , 1 button in each cell. when button pressed should update likepost json have , increment 1. problem getting in updating button post each cell.

i attaching code.

this function button when pressed.

@ibaction func likebtnpressed(_ sender: any) {     if !pressed {         let image = uiimage(named: "like-1.png") uiimage!         likebtn.setimage(image, for: .normal)         pressed = true     } else     {         let image = uiimage(named: "liked.png") uiimage!         likebtn.transform = cgaffinetransform(scalex: 0.15, y: 0.15)          uiview.animate(withduration: 2.0,                        delay: 0,                        usingspringwithdamping: 0.2,                        initialspringvelocity: 6.0,                        options: .allowuserinteraction,                        animations: { [weak self] in                         self?.likebtn.transform = .identity             },                        completion: nil)         likebtn.setimage(image, for: .normal)         pressed = false         likeupdateserver()      }   } 

this function updating :

func likeupdateserver() {     let userdefaults = userdefaults.standard     let value  = userdefaults.string(forkey: "api_token")     let api_token : string! = value     let post_id = "9231"     print(api_token)     print(post_id)       var request = urlrequest(url: url(string: "\(url_base)\(likepost)")!)     request.httpmethod = "post"     let poststring = "api_token=\(api_token!)&&post_id=\(post_id)"     request.httpbody = poststring.data(using: .utf8)     let task = urlsession.shared.datatask(with: request) { data, response, error in         guard let data = data, error == nil else {                                                 // check fundamental networking error             print("error=\(string(describing: error))")             print("cant run")             return         }          if let httpstatus = response as? httpurlresponse, httpstatus.statuscode != 200 {           // check http errors             print("statuscode should 200, \(httpstatus.statuscode)")             print("response = \(string(describing: response))")            }         else {             let responsestring = string(data: data, encoding: .utf8)             print("responsestring = \(string(describing: responsestring))")         }     } } 

this cellforrowat function :

func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell : postdatatableviewcell = self.feedtable.dequeuereusablecell(withidentifier: "contentviewreuse") as! postdatatableviewcell     let post = posts[indexpath.row]     print(post)     cell.configurecell(post : post)     return cell } 

if request parameters right after making datatask need resume it, @ end of likeupdateserver method should add line

task.resume() 

after create task, must start calling resume() method.

you can find indexpath of cell using following code.

let point : cgpoint = sender.convert(.zero, tableview) let indexpath = tableview!.indexpathforitem(at: point) 

add code in button click method , using indexpath can find postid.

also, should flag liked cells (i hope return server) once liked cell can update in local post model.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -