gpt4 book ai didi

ios - 嵌入在 UITableviewCell 中的 UICollectionView 中的 Segue,Swift

转载 作者:行者123 更新时间:2023-11-28 15:18:40 25 4
gpt4 key购买 nike

我在 UITableViewCell 中制作了一个 UICollectionView,它工作得很好。我唯一的问题是,我无法在 didSelectItemAt 方法上执行 Segue 到另一个 ViewController

我知道我必须从 TableViewController 执行它,我在 Storyboard 上做了一个 segue,我尝试了多种可能性,但由于某些原因它不起作用。

这是我的 TableviewController:

import UIKit
import RealmSwift
import SwiftyJSON

class HomeVTwoTableViewController: UITableViewController {

let realm = try! Realm()
let headers = ["1","2","3"]

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
self.tableView.separatorStyle = .none
}


//MARK: Custom Tableview Headers
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return headers[section]
}

//MARK: DataSource Methods
override func numberOfSections(in tableView: UITableView) -> Int {
return headers.count
}

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

//Choosing the responsible PrototypCell for the Sections
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellBig", for: indexPath) as! HomeVTwoTableViewCell
cell.update()
return cell
} else {
return UITableViewCell()
}
}
// This on of my tries to perform a segue
func liveCellSelected() {
performSegue(withIdentifier: "showChat", sender: nil)
}
}

这里是我的 TableViewCell 和嵌入的 CollectionView:

import UIKit
import RealmSwift



class HomeVTwoTableViewCell: UITableViewCell{

var liveCommunities: Results<Community>?

@IBOutlet weak var collectionView: UICollectionView!

override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
}

extension HomeVTwoTableViewCell:
UICollectionViewDataSource,UICollectionViewDelegate {

func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return(liveCommunities?.count)!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellBig", for: indexPath) as? HomeVTwoCollectionViewCell else
{
fatalError("Cell has wrong type")
}
//removes the old image
cell.imageView.image = UIImage(named: "No Image")
cell.titleLbl.text = nil

//set url and Picture
url = (liveCommunities?[indexPath.row].pictureId)!
let name : String = (liveCommunities?[indexPath.row].communityName)!
let channelName : String = (liveCommunities?[indexPath.row].channelName)!

cell.titleLbl.text = name
cell.senderLbl.text = channelName
cell.imageView.downloadedFrom(link :"someSecretUrl")

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCommunity = (liveCommunities?[indexPath.row].communityId)!
HomeVTwoTableViewController().liveCellSelected()
}
}

我找到了 another question具有类似的主题,但无法在不对现有委托(delegate)产生问题的情况下实现委托(delegate)协议(protocol)。

也许这是一个明显的错误,但我看不到。提前致谢。

最佳答案

你正在用这个实例化你的家庭 Controller 的一个新实例:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCommunity = (liveCommunities?[indexPath.row].communityId)!
HomeVTwoTableViewController().liveCellSelected()
}

你应该做的是通过一个委托(delegate)来实现它,或者你将你的 collectionview 委托(delegate)移动到主 Controller

protocol CellCollectionViewDelegate: class{
func didselect()
}

然后你在单元格和你的家庭 Controller 中实现委托(delegate)

关于ios - 嵌入在 UITableviewCell 中的 UICollectionView 中的 Segue,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46400738/

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