TableView Swift内部有多个CollectionView

时间:2020-04-08 18:08:53

标签: swift

我有一个Tableview,我有一个collectionView。 CollectionView内部有imageView。如您在当前照片中所见,分别创建了两个TableView。但是此tableView内部的CollectionViews中的数据是相同的。重要提示:我有一个collectionView。 CollectionView是根据tableView Count创建的。 collectionView的hps具有相同的数据。我想将其他数据上传到CollectionViews,该怎么做?有一个众所周知的用于包皮环切术和婚礼的tableView,但是CollectionView的内容是相同的。我必须做不同的事情。

例如,,我想将davetiyefilee Array中的数据添加到1.CollectionView。 2.我想将davetiyefilee2 Array中的数据添加到CollectionView

enter image description here enter image description here

UITableViewCell

class CategoryRow : UITableViewCell {
    @IBOutlet weak var firstColView: UICollectionView!
    var data = [String]()
    var collectionData = [[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 rowValue = collectionData[indexPath.row]
        for i in 0..<rowValue.count {

           let deneme = collectionData[firstColView.tag][indexPath.item]
            let urlNew = URL(string: deneme)
            cell.denemeImage.sd_setImage(with: urlNew)
               }
        return cell
    }
    func setData(data: [String])
    {
        self.data = data
        self.firstColView.reloadData()
            }

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

MainView

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 = [[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.append([s])
                DispatchQueue.main.async { self.tableViewKategoriler.reloadData()  } } } }
 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.davetiyefile2.append(s)
                  self.collectionData.append([s])
                     DispatchQueue.main.async {
self.tableViewKategoriler.reloadData()
                                                   } } } }}

1 个答案:

答案 0 :(得分:0)

Swift的Views拥有方便的属性,称为标记。您可以为每个CollectionViews分配一个唯一的标签,然后根据以下条件添加数据:

if collectionView.tag == 0 { 

   \\ your first collection view data assignment

} else if collectionView.tag == 1 { 

   \\ your second collection view data assignment

collectionView是指上面的collectionView函数给定的参数。您可以使用情节提要分配标签,但是如果您有很多collectionView,最好以编程方式进行。一篇涵盖以下内容的好文章:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

相关问题