ios - Trouble indexing into array within asynchronous call, Swift -
i'm trying create scrollview of "match" object images correlates array of "match" objects within view controller, if tap onto image of "match" in scrollview, can take index of image in minimatchescontainer, , use access match object within array that image corresponds to. tried going for-loop, problem since i'm getting match images server asynchronously, calls return out of order , containerview indexes off (i've added image of console's print statemen show mean). i'm @ bit of impasse, , appreciate advice go here. should change approach? there i'm missing? code added below.
//function create contentscrollview minimyatches func setupminicontentscroll(contentscroll: uiscrollview) { let scalar:double = 4/19 let contentviewdimension = contentscroll.frame.width * cgfloat(scalar) let contentscrollwidth = cgfloat(localuser.matches.count) * (contentviewdimension + cgfloat(12)) - cgfloat(12) let matchmanager = matchesmanager() index in 0..<localuser.matches.count { let match = localuser.matches[index] print("match index: \(index), match @ index: \(match.itemname)") matchmanager.retrievematchthumbnail(match) { img, error in if let img = img { //create mini matches views let xorigin = index == 0 ? 12 : cgfloat(index) * contentviewdimension + (cgfloat(12) * cgfloat(index) + cgfloat(12)) let contentframe = cgrectmake(xorigin, 10, contentviewdimension, contentviewdimension) let contentview = self.makeminicontentview(contentframe, image: img, matchedprice: match.matchedprice) let tap = uitapgesturerecognizer(target: self, action: #selector(browseviewcontroller.toggleiteminfo(_:))) contentview.addgesturerecognizer(tap) self.minimatchcontainer.append(contentview) print("minimatchcontainer index: \(self.minimatchcontainer.indexof(contentview)), match @ index: \(match.itemname)") //update contentscrollview dispatch_async(dispatch_get_main_queue()) { let contentlabelframe = cgrect(x: xorigin, y: contentframe.height + 15, width: contentframe.width, height: 20) let contentlabel = self.makeminicontentlabel(contentlabelframe, itemname: match.itemname) let pricelabel = self.makeminipricelabel(contentframe, matchedprice: match.matchedprice) contentscroll.addsubview(contentview) contentscroll.addsubview(contentlabel) contentscroll.addsubview(pricelabel) contentscroll.contentsize = cgsizemake(contentscrollwidth + cgfloat(16), contentscroll.frame.height) } } } } }
i try:
print("minimatchcontainer index: \(index), match @ index: \(match.itemname)")
instead of:
print("minimatchcontainer index: \(self.minimatchcontainer.indexof(contentview)), match @ index: \(match.itemname)")
and create content views outside retrievematchthumbnail
, updating inside.
Comments
Post a Comment