I'm trying to use a gridview inside a tableView. But i have a problem when i load images async.
I have N Table's cell with different number of collection's cells. how can i develop it?
I'm using this, but i have some problems when i reload and load different number of items...
i load the data -> tablereload -> collectionviewreload ---------^ Give more info
Suggestions of best practice?
Thanks!
myfunction:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (columna>0){
// println("adasdasd \(columna)")
return columna! as Int}
else{
return 0
}}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GridFotosCell", forIndexPath: indexPath) as GridFotosCell
// Add a check to make sure this exist
// Check our image cache for the existing key. This is just a dictionary of UIImages
//var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
if( AlbumArray[fila!].albumfotoslinks?.count > indexPath.row) {
if( AlbumArray[fila!].albumfotoslow?[indexPath.row] == nil ) {
// If the image does not exist, we need to download it
var link = AlbumArray[fila!]
var finalink = link.albumfotoslinks?[indexPath.row]
finalink = finalink!.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil)
finalink = finalink!.stringByReplacingOccurrencesOfString("+", withString: "%2B", options: NSStringCompareOptions.LiteralSearch, range: nil)
var urlfinal = NSURL(string: "x")
//println(urlfinal!)
// Download an NSData representation of the image at the URL
let request: NSURLRequest = NSURLRequest(URL: urlfinal!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
imageneslow.append(UIImage(data: data)! as UIImage) << i need 2 dimension?
cell.GridFoto.image = imageneslow.last
/*
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = self.TableAlbum.cellForRowAtIndexPath(indexPath) {
cellToUpdate.imageView?.image = (UIImage(data: data)! as UIImage)
}
})*/
// Store the image in to our cache
// AlbumArray[fila!].albumfotoslow! = imagenes
}
else {
println("Error: \(error.localizedDescription)")
}
})
}
else {
//dispatch_async(dispatch_get_main_queue(), {
// if let cellToUpdate = TableAlbum.cellForRowAtIndexPath(indexPath) {
// cellToUpdate.imageView?.image = image
//}
cell.GridFoto.image = imageneslow.last
}
}
// println( AlbumArray[fila!].albumfotoslow?[indexPath.row])
//println(cell.GridFoto.image?.debugDescription)
//println("reloadgrid")
NSLog("return de celda")
NSLog(String.convertFromStringInterpolationSegment(fila))
return cell