Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
TableView内部的多个CollectionView-Swift_Swift - Fatal编程技术网

TableView内部的多个CollectionView-Swift

TableView内部的多个CollectionView-Swift,swift,Swift,我在TableView中使用CollectionView。已将多行添加到此CollectionView。正如您目前在照片中看到的,添加了2个CollectionView。将相同的数据添加到两个CollectionView。如何将数据分别添加到这两个项目中?我想将它组合成一个数组,如下所示,并将第一个数组的项目添加到第一个collectionView,将第二个项目添加到第二个collectionView anaSayfaViewController extension anaSayfaViewCo

我在TableView中使用CollectionView。已将多行添加到此CollectionView。正如您目前在照片中看到的,添加了2个CollectionView。将相同的数据添加到两个CollectionView。如何将数据分别添加到这两个项目中?我想将它组合成一个数组,如下所示,并将第一个数组的项目添加到第一个collectionView,将第二个项目添加到第二个collectionView

anaSayfaViewController

extension anaSayfaViewController : UITableViewDelegate { }

extension anaSayfaViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return kategoriIsımYeni[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return kategoriIsımYeni.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow      
        cell.setData(data: davetiyefilee)
        cell.setDataa(collectionData: collectionData)
        return cell
    }

}

  class anaSayfaViewController: UIViewController, UISearchBarDelegate {


    var collectionData: [Int: [String]] = [:]

 var davetiyefilee = [String]()
    var davetiyefilee2 = [String]()
 @objc func davetiyeCEK1(){

   if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
                 for review in baslik {
                     if let soru_baslik = review["davetiyefilee"] as? String {
                       let s = String(describing: soru_baslik)
                         self.davetiyefilee.append(s)
                         self.collectionData[0] = self.davetiyefilee
                            DispatchQueue.main.async {                       self.tableViewKategoriler.reloadData()
                                                   } } } } }
 @objc func davetiyeCEK1(){

   if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
                 for review in baslik {
                     if let soru_baslik = review["davetiyefilee"] as? String {
                       let s = String(describing: soru_baslik)
                         self.davetiyefilee.append(s)
                         self.collectionData[0] = self.davetiyefilee2
                            DispatchQueue.main.async {                       self.tableViewKategoriler.reloadData()
                                                   } } } } }
UITableViewCell

class CategoryRow : UITableViewCell {
    @IBOutlet weak var firstColView: UICollectionView!
    var data = [String]()
    var collectionData: [Int: [String]] = [:]

}
extension CategoryRow : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  collectionData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellDav1", for: indexPath) as! anaSayfaCell
        let urlNew = URL(string: collectionData[indexPath.item]![indexPath.row])
                           cell.denemeImage.sd_setImage(with: urlNew)


        return cell
    }



    func setData(data: [String])
    {
        self.data = data
        self.firstColView.reloadData()
            }

    func setDataa(collectionData: [Int: [String]])
    {
        self.collectionData = collectionData
        self.firstColView.reloadData()
            }

}


    extension CategoryRow : UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let itemsPerRow:CGFloat = 4
            let hardCodedPadding:CGFloat = 5
            let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
            let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
            return CGSize(width: itemWidth, height: itemHeight)
        }

    }

根据数组计数设置集合视图计数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        switch collectionView{
            case colView1 : return dataArray1.count
            case colView2 : return dataArray2.count
            default : retun 0
        }
    }
根据集合视图获取图像url

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellDav1", for: indexPath) as! anaSayfaCell

    switch collectionView{
        case colView1 :
         let urlNew = URL(string: dataArray1[indexPath.row].url)
         cell.denemeImage.sd_setImage(with: urlNew)
        case colView2 : 
          let urlNew = URL(string: dataArray2[indexPath.row].url)
          cell.denemeImage.sd_setImage(with: urlNew)
        default : break
    }
    return cell
}

有一个collectionView。单元格项在此项下形成。因此,这个案子毫无用处。