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

标签 ios swift uicollectionview uicollectionviewcell alamofire

几天前我一直在尝试解决我的问题,我在网上搜索了很多解决方案,但都没有用。问题……

我正在开发一个 iOS 项目,用户可以在其中拖放项目,项目将根据它们移动的顺序获得优先级。 为了拖放目的,我使用了一个名为 DragDropColelctionView 的库

我创建了一个静态数据以显示在 Collectionview 单元格中。 CollectionView Cell 包含一个图像和一个标签。我在 ViewDidLoad 中创建了这个静态数据,它在我的 ViewController 中显示了带有项目的 CollectioViewCell。但是,当我尝试将动态数据放入集合单元格时,cellForItemAtIndexPath 没有被调用。为了获取数据,我正在使用 Alamofire,我注意到当我用动态数据填充我的模型时,它永远不会被调用。但是,当我创建静态数据并在 Alamofire Scope 外部调用时,cellForItemAtIndexPath 被调用,或者它仅在我的静态数据上被调用。

这是我的模型

class GroupsModel {

    var ItemName            : String?
    var ItemPicture  : UIImage?


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

        }

}

还有我的 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;
                }
        }

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       
}

最佳答案

在响应底部添加collectionView?.reloadData()

像这样

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

关于ios - 使用 Alamofire 调用服务器时未调用 UICollectionView cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37565723/

相关文章:

ios - UICollectionViewCell 和 UICollectionReusableView 有什么区别?

ios - 如何检查 iOS 设置是否允许权限

ios - 按需资源上是否允许存档文件?

php - 不要从正在运行的 iOS 发布查询

ios - ARC 的 CoreAnimation 内存问题

ios - 无法取消执行 block 的NSTimer

ios - 更新到 8.0 版本后 Xcode 链接错误

ios - UICollectionView 断言失败

ios - 在单元格之间的 UICollectionView 中设置/覆盖填充

ios - UICollectionView 自动调整高度