ios - 无法将正确的数据从 UITableView 内嵌的 UICollectionView 发送到 ViewController Swift3

标签 ios uitableview swift3 uicollectionview

我正在使用嵌入在 UITableView 中的 UICollectionView。当用户点击 UITableView 内的 UICollectionViewCell 时,我正在播放音乐,但是当 UITableView 第一次加载时,数据变量为 nil,当用户点击 UICollectionViewCell 时,传递了 nil 值。当用户再次点击 UICollectionViewCell 时,它会显示该 UICollectionViewCell 的实际值。

这是我的代码:

//UITableViewCell

import UIKit

class CategoryRow : UITableViewCell {

 @IBOutlet var collectionView: UICollectionView!

var vcInstance: newHomeTableViewController?
var parentViewController: UIViewController? = nil


 var didSelectAction: (() -> Void )?

 }

extension CategoryRow : UICollectionViewDataSource , UICollectionViewDelegate{

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

    return imageurl.count
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "newHomeCell1", for: indexPath) as! CollectionViewCell

     cell.image1.sd_setImage(with: URL(string: imageurl[indexPath.row]))

    return cell

}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //print("cell no: \(indexPath.row) of collection view: \(collectionView.tag)")

    didSelectAction!()

     vcInstance?.musicName = RemoteModel.sharedInstanceRemoteModel.singleGuidedTrandingTrackname[indexPath.row]
     vcInstance?.musicUrl = RemoteModel.sharedInstanceRemoteModel.singleGuidedTrandingTrackurl[indexPath.row]

    }

   }

   extension CategoryRow : UICollectionViewDelegateFlowLayout {

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let _: CGFloat = 10
    return CGSize(width : (collectionView.bounds.width)/2 - 10, height : collectionView.bounds.height - 10)
   }

  }

//TableViewController:-

import UIKit

class newTableViewController: UITableViewController {

 var musicName : String?
 var musicUrl : String?

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{

    return 70
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

         let cell : CategoryRow = tableView.dequeueReusableCell(withIdentifier: "cell")as! CategoryRow

        cell.vcInstance = self
         cell.parentViewController = self

         cell.didSelectAction = {
             self.performSegue(withIdentifier: "homePlayer", sender: indexPath)
         }

        return cell
    }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "homePlayer" {

        let playerVC = segue.destination as! playerViewController

        playerVC.UrlAudio = musicUrl
        playerVC.songTittle = musicName

       }
    }

  }

除了第一次加载 UITableView 时,数据变量为 nil 外,这工作得很好。 提前致谢。

最佳答案

请引用下面的代码,我已经为您创建了一个演示,符合您的要求,希望对您有所帮助!

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{


     @IBOutlet weak var tblView: UITableView!
     @IBOutlet weak var outputLbl: UILabel!

     var content = ["Good Morning1","Good Morning2","Good Morning3","Good Morning4","Good Morning5"]

     override func viewDidLoad() {
          super.viewDidLoad()
          tblView.rowHeight = 400
     }

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

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath ) as! aTableViewCell
          cell.aCollection.reloadData()
          return cell
     }
}

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

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! ACollectionViewCell
          cell.aLabel.text = content[indexPath.item]
          return cell
     }

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          let collectionCell = collectionView.cellForItem(at: indexPath) as! ACollectionViewCell
          let selectedData = collectionCell.aLabel.text
          outputLbl.text = selectedData
     }

}

class aTableViewCell : UITableViewCell{

     @IBOutlet weak var aCollection: UICollectionView!
}

关于ios - 无法将正确的数据从 UITableView 内嵌的 UICollectionView 发送到 ViewController Swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058830/

相关文章:

ios - -Objc 其他链接器标志给出重复符号错误

ios - 如何在 iOS 中使用 FieldValue.serverTimestamp() 作为模型类 - swift

ios - com.apple.developer.healthkit.access 警告

ios - 如何调整 UITableViewController tableView 的大小?

ios - iOS 中使用 Swift 3 的 CollectionView 动态高度

ios - 具有自动布局的 GPUImageView

ios - 过滤 NSFetchedResultsController 以仅获取具有某种关系的对象

iphone - 如何检测是否在 xcode 7 中点击了表格 View 单元格

json - URLSession 错误

ios - 我不知道如何让它们加减,这是有办法的。基本上我想要 1"+"3 = 4 但我得到 1+3