我在UITableView中使用UICollectionView Embedded。当用户点击UITableView内的UICollectionViewCell时,我正在播放音乐,但是当第一次加载UITableView时,数据var为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,否则数据var为零。 提前谢谢。
答案 0 :(得分:1)
请参考下面的代码,我已经为您创建了一个演示,符合您的要求,希望它有所帮助!
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!
}
答案 1 :(得分:0)
根据您的代码,第一次,值为nil
,为避免这种情况,您必须重新加载UICollectionViewCell
中嵌入的UITableViewCell
,
所以重新加载UICollectionViewCell
,在tableView' cellForRowAt:
写一下,
yourCollectionView.reloadData()
应该让您的代码正常工作!