Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios uicollectionview未显示单元格_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios uicollectionview未显示单元格

Ios uicollectionview未显示单元格,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我从url获取的数据是jsonstring。在我的代码中,titles变量是我的url图像的数组 private let reuseIdentifier = "cell_supporters" class SupportersCollectionViewController: UICollectionViewController { var ids = [String]() var titles = [String]() @I

我从url获取的数据是
json
string。在我的代码中,
titles
变量是我的
url
图像的数组

private let reuseIdentifier = "cell_supporters"

class SupportersCollectionViewController: UICollectionViewController {
    var ids             = [String]()
    var titles          = [String]()

    @IBOutlet var collection_supporters: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collection_supporters.delegate = self

        // Register cell classes
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        getSupporters()
    }

    // MARK: UICollectionViewDataSource

//    override func numberOfSections(in collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.titles.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! SupportersCollectionViewCell

        print("Hello collectionView")

        let url_img = URL(string: self.titles[indexPath.row])!

        print(url_img)
        cell.img_logo.af_setImage(
            withURL: url_img
        )

        return cell
    }

    func getSupporters() {
        RestApiManager.sharedInstance.getSupporters { (json: JSON) in
            if let results = json.array {
                for entry in results {
                    // print(entry["title"])
                    //self.ids.append(entry["id"].string!)
                    self.titles.append(entry["title"].string!)
                }

                print(self.titles.count)

                DispatchQueue.main.async{
                    self.collection_supporters.reloadData()
                }
            }
        }
    }
}

在我的代码中:

print(self.titles.count) // it shows 5
但是:


ViewDidLoad()方法中添加此行:

self.collectionView!.delegate = self
将其更改为:

private let reuseIdentifier = "cell_supporters"

class SupportersCollectionViewController: UICollectionViewController {
    var ids             = [String]()
    var titles          = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.register(SupportersCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        getSupporters()
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.titles.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! SupportersCollectionViewCell

        print("Hello collectionView")

        let url_img = URL(string: self.titles[indexPath.row])!

        print(url_img)
        cell.img_logo.af_setImage(
            withURL: url_img
        )

        return cell
    }

    func getSupporters() {
        RestApiManager.sharedInstance.getSupporters { (json: JSON) in
            if let results = json.array {
                for entry in results {
                    // print(entry["title"])
                    //self.ids.append(entry["id"].string!)
                    self.titles.append(entry["title"].string!)
                }

                print(self.titles.count)

                DispatchQueue.main.async{
                    self.collectionView!.reloadData()
                }
            }
        }
    }
}
首先,您不需要这样做:

@IBOutlet var collection_supporters: UICollectionView!
因为您的视图控制器是从UICollectionViewController继承的

此外,我还将UICollectionViewCell类的寄存器更改为此,以便在创建单元格时不会崩溃:

self.collectionView!.register(SupportersCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

如果您正在为布局使用默认的
UICollectionViewFlowLayout
类,则可以尝试实现委托方法,该方法返回
collectionView
中每个项目的大小。如果
UICollectionView
没有大小信息或大小为零,则它根本不会调用
cellForItemAt
dataSource方法

尝试添加以下内容:

extension SupportersCollectionViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 40, height: 40) // Return any non-zero size here
    }
}

不要注册单元类。从viewDidLoad函数中删除以下行:

 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

这是一个猜测,因为您正在将
委托
分配给
集合
但您正在将单元格注册到
self.collectionView
。也许可以尝试将单元格注册到
collection\u-supports
?既然这样做并不坏,为什么你要否决我的答案?我只是根据你提供的代码试着帮你@S.M_eMamian虽然没有解决您的特定问题,但这确实是一个问题
self.collection\u支持者
self.collectionView
之间有什么区别?但是您的问题应该是
收集支持者。datasource
设置为
self
(至少在InterfaceBuilder中,因为它不是由您提供的代码完成的)。我不知道为什么这个答案为-1,但它帮助了我!这里同样不清楚它是如何帮助的,为什么要删除这一行,为什么不注册?
 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)