[英]How to show images in horizontal collection view inside tableview swift 5
I want to show image using sdWebImage to an image inside collectionView Cell which is inside tableView cell.我想使用 sdWebImage 将图像显示到 tableView 单元内的 collectionView Cell 内的图像。 How to do it?
怎么做? I have array of images and I want to show it in collection view cell this is what I tried
我有一系列图像,我想在集合视图单元格中显示它这是我尝试过的
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let imgUrl = (myArray[indexPath.row] as? String)
{
//myArray = image array
if let url = URL(string: imgUrl as! String) {
cell.imgView.sd_setImage(with: url, placeholderImage: UIImage(named: "product.png"), options: .lowPriority)
}
}
}
I already set我已经设置
func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
collectionVIew.delegate = dataSourceDelegate
collectionVIew.dataSource = dataSourceDelegate
collectionVIew.tag = row
collectionVIew.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardTableViewCell") as! DashboardTableViewCell
cell.delegate = self
cell.selectedIP = indexPath
cell.collectionVIew.tag = indexPath.row
cell.collectionVIew.reloadData()
return cell
}
But I don't know how to show images inside collection view in particular tableview section.但我不知道如何在特定 tableview 部分的集合视图中显示图像。 Please Help.
请帮忙。
Set Collection view Deleagte and dataSource inside the table view cellforRow method在表视图 cellforRow 方法中设置 Collection 视图 Deleagte 和 dataSource
collectionview.delegae = self
collectionview.datasource = self
As mention by @AjinkyaSharma You have to rearrange your data structure, where it would be an array of arrays.正如@AjinkyaSharma 所提到的,您必须重新排列您的数据结构,它将是 arrays 的数组。
let myImages: [[String]] = [
["image_url_0_0", "image_url_0_1", "image_url_0_2", "image_url_0_3"],
["image_url_1_0", "image_url_1_1", "image_url_1_2"],
["image_url_2_0", "image_url_2_1", "image_url_2_2", "image_url_2_3", "image_url_2_4"],
["image_url_3_0", "image_url_3_1"]
]
Now you tableview cellForRowAt
will set the required array of images for collectionView现在您的 tableview
cellForRowAt
将为 collectionView 设置所需的图像数组
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myImages.size
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DashboardTableViewCell") as! DashboardTableViewCell
cell.delegate = self
cell.selectedIP = indexPath
cell.collectionVIew.tag = indexPath.row
cell.myArray = myImages[indexPath.row]
cell.collectionVIew.reloadData()
return cell
}
Now you need to use your myArray array to display your collectionView cells现在您需要使用 myArray 数组来显示您的 collectionView 单元格
One of your url is not able to satisfy if let condition.如果让条件,您的 url 之一不能满足。 Set default image to nil to make sure not to show any previous data.
将默认图像设置为 nil 以确保不显示任何以前的数据。 Try This:
尝试这个:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.imgView.image = nil // set default image nil
if let imgUrl = (myArray[indexPath.row] as? String)
{
//myArray = image array
if let url = URL(string: imgUrl as! String) {
cell.imgView.sd_setImage(with: url, placeholderImage: UIImage(named: "product.png"), options: .lowPriority)
}
}
}
Try With Below Code试试下面的代码
class YourCustomizeTableViewCell: UITableViewCell {
let collectionView: CollectionView
...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as! YourCustomizeTableViewCell
cell.collectionView.tag = indexPath.row
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it?
cellCharLabel?.text =
Languages.sharedInstance.alphabets[collectionView.tag].set[indexPath.row].char
...
return cell
}
func setCollectionViewDataSourceDelegate<D:UICollectionViewDataSource & UICollectionViewDelegate>(datasourcedelegate : D, forRow row:Int){
collectionVIew.delegate = datasourcedelegate
collectionVIew.dataSource = datasourcedelegate
collectionVIew.tag = row
collectionVIew.isPagingEnabled = row == 0 ? false : true
collectionVIew.accessibilityHint = row == 0 ? "category" : "offers"
collectionVIew.reloadData()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
guard let cell = cell as? DashboardTableViewCell else {
return
}
cell.setCollectionViewDataSourceDelegate(datasourcedelegate: self, forRow: indexPath.section)
}
extension HomeViewController : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PopularCollectionViewCell.identifier, for: indexPath) as? PopularCollectionViewCell else {
return UICollectionViewCell()
}
cell.backgroundColor = .clear
cell.setValue(data: popularItemList[indexPath.row])
return cell
}
}
extension HomeViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:120, height: itemWidth + 82)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.