gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:38:07 25 4
gpt4 key购买 nike

我正在使用嵌入在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com