【发布时间】:2019-02-09 06:56:30
【问题描述】:
我在帖子对象数组中获得了一个图像 url 数组我想在表格视图中显示此帖子数组并将图像显示为表格视图中的集合视图。我怎样才能enter image description here做到这一点?我厌倦了使用 int i 作为指标,但它不起作用。
这里是代码
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeTableViewCell
cell.postsText.text = postLists[indexPath.row].postText
i = indexPath.row
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (postLists[i].imageUrlList?.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! PhotosCollectionCell
let url = postLists[i].imageUrlList![indexPath.row]
let imgUrl = URL(string: url)
URLSession.shared.dataTask(with: imgUrl!, completionHandler: { (data, response, error) in
if error != nil {
// if download hits an error, so lets return out
print(error)
return
}
// if there is no error happens...
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // in half a second...
cell.postPhoto.image = UIImage(data: data!)
}
}).resume()
return cell
}
【问题讨论】:
-
检查你的数组数据
标签: swift tableview collectionview