gpt4 book ai didi

swift - 我该如何解决有关委托(delegate)和协议(protocol)的问题?

转载 作者:行者123 更新时间:2023-11-28 08:30:32 25 4
gpt4 key购买 nike

我想在 Collection View 中使用 TableView 单元格的数据。那些在同一个 Controller 中。我怎样才能做到这一点 ?当我尝试这个时,委托(delegate)为零,什么也没有发生。没关系 CellForRowAtIndexPath,numberOfRowsInSection 方法。

protocol DataDelegate {
func dataInfo(survey:Survey)


}

import UIKit

class SurveyViewControllerLeftTableView: UITableView,UITableViewDataSource,UITableViewDelegate {

var survey = [Survey]()

var myDelegate : DataDelegate?

override func awakeFromNib() {
self.dataSource=self
self.delegate=self

}



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


myDelegate?.dataInfo(survey[(indexPathForSelectedRow?.row)!])

}

}


import UIKit

class BottomCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate,DataDelegate {


var surveyBottomCollection = [Survey]()



override func awakeFromNib() {
super.awakeFromNib()


self.delegate = self
self.dataSource = self
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return surveyBottomCollection.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell : BottomCollectionViewCell = dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BottomCollectionViewCell

cell.surveyQuestionNumberLabel.text = surveyBottomCollection[indexPath.row].questionNumber
cell.surveyQuestionDescriptionLabel.text = surveyBottomCollection[indexPath.row].questionDescription

return cell

}

func dataInfo(survey: Survey) {
print(survey.questionDescription)

surveyBottomCollection.append(survey)
self.reloadData()
}

最佳答案

据我所知,您需要创建 UITableView自定义单元格或需要在 storyboard 中创建单元格然后使用以下代码获取 CollectionView将数据放入表格 View 单元格。

同时选择任意单元格tableview:didSelect将调用并执行 select 函数,您需要将 tableview 数组对象相互添加 Array并在你身上使用那个数组 Collection查看单元格。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

// Replace Beow line with your costume cell outlets
cell.textLabel?.text = self.items[indexPath.row]

//Like this Probably same like collection view or what ever you want to rename it in TableViewCell
cell.surveyQuestionNumberLabel.text = surveyBottomCollection[indexPath.row].questionNumber
cell.surveyQuestionDescriptionLabel.text = surveyBottomCollection[indexPath.row].questionDescription

return cell
}

更新:

// Use this method to get the selected objects and add them into a new array.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Add Your object with indexPath.row and reload your collectionView here
}

希望上面的代码能帮助你实现你想要的。

关于swift - 我该如何解决有关委托(delegate)和协议(protocol)的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38951039/

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