Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何在tableview swift 5内的水平集合视图中显示图像_Ios_Swift - Fatal编程技术网

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

Ios 如何在tableview swift 5内的水平集合视图中显示图像,ios,swift,Ios,Swift,我想使用sdWebImage向tableView单元格内的collectionView单元格内的图像显示图像。怎么做? 我有一个图像数组,我想在集合视图单元格中显示它 这就是我试过的 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let imgUrl = (myArray[indexPath

我想使用sdWebImage向tableView单元格内的collectionView单元格内的图像显示图像。怎么做? 我有一个图像数组,我想在集合视图单元格中显示它 这就是我试过的


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)

                }
            }
}
我已经准备好了

func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
       collectionVIew.delegate = dataSourceDelegate
       collectionVIew.dataSource = dataSourceDelegate
       collectionVIew.tag = row
       collectionVIew.reloadData()
   }

但我不知道如何在集合视图中的特定tableview部分显示图像。
请提供帮助。

在表视图cellforRow方法中设置集合视图Delegte和数据源

collectionview.delegae = self
collectionview.datasource = self

在表视图cellforRow方法内设置集合视图Delegte和数据源

collectionview.delegae = self
collectionview.datasource = self

正如@AjinkyaSharma所提到的,您必须重新安排您的数据结构,在这里它将是一个数组数组

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"]
]
现在,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
}

现在,您需要使用myArray数组来显示collectionView单元格,正如@AjinkyaSharma所提到的,您必须重新排列数据结构,即数组

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"]
]
现在,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
}

现在,您需要使用myArray数组来显示collectionView单元格

您的url之一无法满足if-let条件。将默认图像设置为nil以确保不显示任何以前的数据。 试试这个:

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)

                }
            }
     }

您的某个url无法满足if-let条件。将默认图像设置为nil以确保不显示任何以前的数据。 试试这个:

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)

                }
            }
     }
试试下面的代码

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
 }
试试下面的代码

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(datasourcedelegate:D,forRow row:Int){
collectionVIew.delegate=datasourcedelegate
collectionVIew.dataSource=datasourcedelegate
collectionVIew.tag=行
collectionVIew.isPaginEnabled=行==0?false:true
collectionVIew.accessibilityHint=行==0?“类别”:“提供”
collectionVIew.reloadData()
} 
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexath)
警卫让单元格=单元格为?仪表板TableViewCell-else{
返回
}
cell.setCollectionViewDataSourceDelegate(datasourcedelegate:self,forRow:indexPath.section)
}
扩展HomeViewController:UICollectionViewDataSource{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回数组.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
guard let cell=collectionView.dequeueReusableCell(使用ReuseIdentifier:PopularCollectionViewCell.identifier,for:indexPath)作为?PopularCollectionViewCell else{
返回UICollectionViewCell()
}
cell.backgroundColor=.clear
cell.setValue(数据:popularItemList[indexPath.row])
返回单元
}
}
扩展HomeViewController:UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:120,高度:itemWidth+82)
}
}
func setCollectionViewDataSourceDelegate(datasourcedelegate:D,forRow row:Int){
collectionVIew.delegate=datasourcedelegate
collectionVIew.dataSource=datasourcedelegate
collectionVIew.tag=行
collectionVIew.isPaginEnabled=行==0?false:true
collectionVIew.accessibilityHint=行==0?“类别”:“提供”
collectionVIew.reloadData()
} 
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexath)
警卫让单元格=单元格为?仪表板TableViewCell-else{
返回
}
cell.setCollectionViewDataSourceDelegate(datasourcedelegate:self,forRow:indexPath.section)
}
扩展HomeViewController:UICollectionViewDataSource{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回数组.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
guard let cell=collectionView.dequeueReusableCell(使用ReuseIdentifier:PopularCollectionViewCell.identifier,for:indexPath)作为?PopularCollectionViewCell else{
返回UICollectionViewCell()
}
cell.backgroundColor=.clear
cell.setValue(数据:popularItemList[indexPath.row])
返回单元
}
}
扩展HomeViewController:UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:120,高度:itemWidth+82)
}
}

到底是什么问题?图像没有显示吗?代理是否未被调用?@AjinkyaSharma-显示的是图像,但每个部分和收集单元中的前3-4个图像随处可见。您必须重新排列数据结构,其中将是一个数组。所以,一个数组表示tableview的单元格数,每个数组都有一个数组表示collectionView的单元格数。@AjinkyaSharma-我不明白。你想说什么?你使用的是什么数据源数组?您在哪里添加了collectionView数据源方法?问题到底是什么?图像没有显示吗?代理是否未被调用?@AjinkyaSharma-显示的是图像,但每个部分和收集单元中的前3-4个图像随处可见。您必须重新排列数据结构,其中将是一个数组。所以,一个数组表示tableview的单元格数,每个数组都有一个数组表示collectionView的单元格数。@AjinkyaSharma-我不明白。你想说什么?你使用的是什么数据源数组?那么您在哪里添加了collectionView数据源方法呢?如果这是问题所在,单元格将不会首先出现在collectionView中。但根据OP,他看不到