Joker Joker - 6 months ago 85
Swift Question

UICollectionView cellForItemAtIndexPath not getting Called When calling server using Alamofire

I have been trying to fix my problem a few days before, I searched over internet got a lot solutions but none worked. The Problem ...

I am working on an iOS project in which the user can drag and Drop items and the items will get priority according to the order they have moved.
For drag and drop purpose I am using a Library called DragDropColelctionView

I created a static data to display inside the Collectionview cell. CollectionView Cell contains an Image and a Label. I created this static data inside ViewDidLoad and it's showing CollectioViewCell with items in my ViewController. But when I tried to put dynamic data's into the Collection cell cellForItemAtIndexPath didn't get's called. For fetching data I am Using Alamofire and I noticed that When I filled my Model With Dynamic Data it's never get called . But when I created a static data and called outside Alamofire Scope the cellForItemAtIndexPath got called or it is getting called only on my static data .

This is My model

class GroupsModel {

var ItemName : String?
var ItemPicture : UIImage?


init?(ItemName : String , ItemPicture : UIImage? )
{ print("Initilaised")
self.ItemName = ItemName
self.ItemPicture = ItemPicture

}

}


And my Alamofire request

Alamofire.request(.GET, App.AppHomeURL() + "Load_Items" , parameters : ["userid": "\(cache.getObjectid())"]).responseJSON
{
response in

switch response.result
{
case .Success(let _result) :
let jsoResponse = JSON(_result)
var _itemName : String = ""
var _itemImage : UIImage
do
{
if let Dataobject = resposne!["Data"] as? NSDictionary
{
if let _items = Dataobject["Items"] as? [NSDictionary]
{
for itemDetails in _items
{
print("Item Name : \(itemDetails["itemName"]!)")

_itemName = itemDetails["itemName"]! as! String _itemImage = UIImage(named: "Sample Profile")!


let itemInfo = GroupsModel(ItemName: _itemName, ItemPicture: ItemPicture)!
//
self.data += [itemInfo]



}
}

else
{
print("No Items")
}
}
}


break;

case .Failure(let _er) :
print("An Error Occured : \(_er)")

break;
}
}


And The CellForRowAtIndexPath Function

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionCell

let _data = data[indexPath.row]
print("Row : \(indexPath.row))")
cell.lblGroupName.text = _data.strgroupName
cell.ItemImage.image = _data.ItemPicture
cell.ItemImage.layer.cornerRadius = cell.ItemImage.frame.width / 2
cell.ItemImage.clipsToBounds = true

return cell
}

Answer

Add collectionView?.reloadData() in bottom of response

like this

Alamofire.request(.GET, App.AppHomeURL() + "Load_Items" , parameters : ["userid": "\(cache.getObjectid())"]).responseJSON
        {
            response in

            switch response.result
            {
            case .Success(let _result) :
                let jsoResponse =  JSON(_result)
                    var _itemName : String = ""
        var _itemImage : UIImage
       do
        {
            if let Dataobject = resposne!["Data"] as? NSDictionary
            {
                if let _items = Dataobject["Items"] as? [NSDictionary]
                {
                    for itemDetails in _items
                    {
                        print("Item Name : \(itemDetails["itemName"]!)")

                        _itemName = itemDetails["itemName"]! as! String _itemImage = UIImage(named: "Sample Profile")!


                        let itemInfo = GroupsModel(ItemName: _itemName, ItemPicture: ItemPicture)!
                        //
                        self.data += [itemInfo]



                    }
                }
                  collectionView?.reloadData()
                else
                {
                    print("No Items")
                }
            }
        }                                   


                break;

            case .Failure(let _er) :
                print("An Error Occured : \(_er)")

                break;
            }
    }
Comments