ios - Swift/Xcode - Search API and compare current value(s) with rest of API - SwiftyJSON & AlamoFire -
firstly, point out, have search function implemented use of search-bar , alamofire. , can download , present data in collectionview
. wondering how implement following:
this api looks like:
students: [ { "firstname": "fred", "lastname": "bloggs", "testscore": "89", }, { "firstname": "frank", "lastname": "thomas", "testscore": "91", }, { "firstname": "joe", "lastname": "morrison", "testscore": "45", }, ]
brief example of api.
so currently, able download information uicollectionview
. struggling pass data have downloaded api detailviewcontroller
. trying pass lastname
, testscore
@ current index path.
i wish following:
when loading detailviewcontroller
wish testscore
of selected indexpath , return other students have similar score 3, 5 , 10. involve comparing current testscore
whole api again, correct?
for example:
if selected frank, similar student fred. frank has test score of 91 , fred has score of 89. due fred's score , frank's score having difference of 3. if select fred, frank appear. due difference being 3.
i hope make's sense.
this current code have far. leave out uicollectionview
code fair straight forward.
homeviewcontroller.swift
class homeviewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate{ @iboutlet weak var collectionview: uicollectionview! @iboutlet weak var searchbar: uisearchbar! var students = [json]() { didset { self.collectionview.reloaddata() } } override func viewdidload() { super.viewdidload() collectionview.delegate = self collectionview.datasource = self searchbar.delegate = self searchbar.placeholder = "search player" searchbar.searchbarstyle = .minimal definespresentationcontext = true notificationcenter.default.addobserver(self, selector: #selector(homeviewcontroller.search(searchtext:)), name: nsnotification.name(rawvalue: "searchresultsupdated"), object: nil) // additional setup after loading view. } func search(searchtext: string) { self.students.removeall() let url = "(api url)" alamofire.request(url, method: .get).responsejson { response in if let results = response.result.value as? [string:anyobject] { let listofstudents = json(results["students"]!).arrayvalue self.students += listofstudents notificationcenter.default.post(name: nsnotification.name(rawvalue: "searchresultsupdate"), object: nil) } } } func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { //#warning incomplete method implementation -- return number of items in section return student.count } func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) { performsegue(withidentifier: "searchview_playerdetailssegue", sender: nil) var detailsviewcontroller = self.storyboard?.instantiateviewcontroller(withidentifier: "detailsviewcontroller") as! studentdetailviewcontroller detailsviewcontroller.studentname = (cannot work!) } func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { let cell: playercollectionviewcell = collectionview.dequeuereusablecell(withreuseidentifier: "studentcell", for: indexpath indexpath) as! studentcollectionviewcell cell.studentnamelabel.text = student[indexpath.row]["name"].stringvalue return cell } deinit { notificationcenter.default.removeobserver(self) } } extension homeviewcontroller: uisearchbardelegate { func searchbarsearchbuttonclicked(_ searchbar: uisearchbar) { search(searchtext: searchbar.text!) dispatchqueue.main.async() { self.collectionview.reloaddata() } } }
i have amended code ease of reading. if there mistakes, it's not actual code.
thank time.
kind regards.
Comments
Post a Comment