简体   繁体   English

如何在 tableview swift 5 内的水平集合视图中显示图像

[英]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.

相关问题 如何在 swift 中使用集合视图显示文件夹中的图像 - How to show images within folder using collection view in swift 如何使用 SDWebImage 在 tableview Swift 中显示图像? - How to use SDWebImage to show images in tableview Swift? 如何使用Swift在IOS中的应用商店中使用自定义标头操作在Tableview中显示Collection View - How to Display Collection View inside tableview with custom header action like app store in IOS using Swift 如何在Swift中将视图控制器的值访问到tableview内的tableview中 - How to access the value of view controller into the tableview inside tableview in Swift 如何在集合视图中保存图像数组Swift 4 - How to save an array of images in a collection view swift 4 如何在Swift中的UICollectionReusableView内部重新加载集合视图 - How to reload collection view inside UICollectionReusableView in swift 如何在Tableview中使用集合视图显示内容 - How to display content using collection view inside a Tableview 在Extension Swift 4.2中更改导航栏,表格视图,集合视图的颜色属性和图像 - Changing colour properties and images for navigation bar, tableview, collection view in Extension Swift 4.2 Swift Collection 视图水平和垂直滚动 - Swift Collection view Horizontal and vertical scroll 如何在集合视图单元格的某一部分中添加多个数组,该集合视图位于tableView单元格内? - How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM