Ios uicollectionview未显示单元格
我从url获取的数据是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
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)