ios - Why is reloadData() not working in my app? -
i'm trying download set of images parse.com uicollectionview. works except reloaddata() function. in app, can hit button , re-enter uicollectionview , of images there. don't know i'm doing wrong reloaddata() function.
var images = [uiimage]() var imagefiles = [pffile]() class collectioncollectionviewcontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource { @ibaction func xy1button(sender: anyobject) { } @ibaction func xy2button(sender: anyobject) { var downloadimages = pfquery(classname: "xy2") downloadimages.wherekey("expansion", equalto: "xy2") downloadimages.findobjectsinbackgroundwithblock { (objects: [anyobject]?, error: nserror?) -> void in if error == nil { println("successfully retrieved \(objects!.count) cards.") if let objects = objects as? [pfobject] { object in objects { imagefiles.append(object["image"] as! pffile) dispatch_async(dispatch_get_main_queue() ^{ self.collectionview.reloaddata() }); //error breakpoint here:'thread 1: exc_bad_instruction (code=exc_i386_invop, subcode=0x0)' } } } else { println("error: \(error!) \(error!.userinfo!)") } } } override func viewdidload() { super.viewdidload() } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return imagefiles.count } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell: cardscollectionviewcell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! cardscollectionviewcell //cell.cardsimg.image = uiimage(named: cards[indexpath.row] string) //return cell imagefiles[indexpath.row].getdatainbackgroundwithblock{ (imagedata: nsdata?, error: nserror?) -> void in if error == nil { let image = uiimage(data: imagedata!) cell.cardsimg.image = image } } return cell } }
the reloaddata need executed in main thread update ui. can try
dispatch_async(dispatch_get_main_queue(), ^{ self.collectionview.reloaddata() }); and @grimxn suggested, should check if collectionview iboutlet connected in storyboard or xib file.
Comments
Post a Comment