简体   繁体   English

使用Alamofire调用服务器时未调用UICollectionView cellForItemAtIndexPath

[英]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. 我正在一个iOS项目中,用户可以在其中拖放项目,并且这些项目将根据其移动顺序获得优先级。 For drag and drop purpose I am using a Library called DragDropColelctionView 出于拖放目的,我使用一个名为DragDropColelctionView的库

I created a static data to display inside the Collectionview cell. 我创建了一个静态数据以显示在Collectionview单元内。 CollectionView Cell contains an Image and a Label. CollectionView单元格包含一个图像和一个标签。 I created this static data inside ViewDidLoad and it's showing CollectioViewCell with items in my ViewController. 我在ViewDidLoad中创建了此静态数据,并在ViewController中显示了带有项目的CollectioViewCell。 But when I tried to put dynamic data's into the Collection cell cellForItemAtIndexPath didn't get's called. 但是,当我尝试将动态数据放入“集合”单元格cellForItemAtIndexPath时,没有得到调用。 For fetching data I am Using Alamofire and I noticed that When I filled my Model With Dynamic Data it's never get called . 为了获取数据,我使用Alamofire,并且注意到当我用动态数据填充模型时,永远不会调用它。 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 . 但是,当我创建静态数据并在Alamofire Scope之外调用时, cellForItemAtIndexPath被调用或仅在我的静态数据上被调用。

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要求

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 CellForRowAtIndexPath函数

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       
}

You're not reloading your UICollectionView after filled the dataSource. 填充数据UICollectionView后,您无需重新加载UICollectionView

use : myCollectionView?.reloadData() 使用: myCollectionView?.reloadData()

Add collectionView?.reloadData() in bottom of response 在响应底部添加collectionView?.reloadData()

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;
            }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView CellForItemAtIndexPath未被调用 - UICollectionView CellForItemAtIndexPath not getting called 在iOS 8中重新加载时未调用UICollectionview cellForItemAtIndexPath - UICollectionview cellForItemAtIndexPath not getting called when reload in iOS 8 UICollectionView cellForItemAtIndexPath方法没有被调用 - UICollectionView cellForItemAtIndexPath method is not getting called 在performBatchUpdates中重新加载collectionview时未调用UICollectionview cellForItemAtIndexPath - UICollectionview cellForItemAtIndexPath not getting called when collectionview reload in performBatchUpdates 不调用UICollectionView的cellForItemAtIndexPath(不使用情节提要) - The cellForItemAtIndexPath for UICollectionView not be called (without using storyboard) 未调用 UICollectionView 的 cellForItemAtIndexPath - UICollectionView's cellForItemAtIndexPath is not being called UICollectionView cellForItemAtIndexPath多次调用 - UICollectionView cellForItemAtIndexPath called many times 在UITabBarController中未调用UICollectionView cellForItemAtIndexPath - UICollectionView cellForItemAtIndexPath Not Called within UITabBarController 使用自定义UICollectionViewLayout子类时,不会调用cellForItemAtIndexPath :. - cellForItemAtIndexPath: is not called when using custom UICollectionViewLayout subclass 调用了UICollectionView cellForItemAtIndexPath,但是数据源为空 - UICollectionView cellForItemAtIndexPath is called but data source is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM