为 indexPath 获取 didSelectItemAt 的值并将其添加到委托/协议以填充 header 单元格
Taking the value on didSelectItemAt for indexPath and add it to a delegate / protocol to populate a header cell
使用协议/委托并从 didSelectItemAt (collectionViews) 检索数据。
// This class have the data
// WhereDataIs: UICollectionViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Pass some data from the didSelect to a delegate
let test = things[indexPath.item]
guard let bingo = test.aThing else { return }
print("bingo: ", bingo)
那个宾果游戏正在打印我需要的值。那里很好。
现在,我不能在那个函数内部使用协议的方法,那是糟糕的执行所以编译器或 Xcode 说嘿,你将把该方法声明为常规方法,而不是嵌套方法方式。
//Bridge
protocol xDelegate {
func x(for headerCell: HeaderCell)
}
//This is the class that need the data
//class HeaderCell: UICollectionViewCell
var xDelegate: xDelegate?
//it's init()
override init(frame: CGRect) {
super.init(frame: frame)
let sally = WhereDataIs()
self.xDelegate = sally
xDelegate?.x(for: self)
}
// This extension is added at the end of the class WhereDataIs()
// Inside of this class is it's delegate.
var xDelegate: xDelegate? = nil
extension WhereDataIs: xDelegate {
func x(for headerCell: HeaderCell) {
//Smith value will work because it's dummy
headerCell.lbl.text = "Smith"
// What I really need is instead of "Smith" is the value of didselectItemAt called bingo.
headerCell.lbl.text = bingo
}
}
欢迎任何愿意在这方面指导我的人。
我认为您可以将其添加到 didSelectItemAt indexPath
中当前代码的末尾:
guard let header = collectionView.supplementaryView(forElementKind: "yourElementKind", at: indexPath) as? HeaderCell else { return }
header.lbl.text = bingo
collectionView.reloadData()
编辑:暂时保留其他所有内容,以确保您首先获得好的结果。
让我知道这是否有效,很高兴回来查看。
不使用委托,但它会起作用
解决者:
1) 转到collectionView的控制器。创建一个新变量来存储项目。
// Pass the item object into the xController so that it can be accessed later.
//(didSelectItemAt)
xController.newVar = item
//xController class: UICollectionView...
// MARK: - Properties
var newVar: Thing?
最后,在那个 class 中,您应该有方法 viewForSupplementaryElementOfKind,您可以在其中注册 HeaderCell,然后只需添加...
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! HeaderCell
header.lbl.text = newVar.whatEverYourDataIs
有点笼统,但很管用。 :)
使用协议/委托并从 didSelectItemAt (collectionViews) 检索数据。
// This class have the data
// WhereDataIs: UICollectionViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Pass some data from the didSelect to a delegate
let test = things[indexPath.item]
guard let bingo = test.aThing else { return }
print("bingo: ", bingo)
那个宾果游戏正在打印我需要的值。那里很好。
现在,我不能在那个函数内部使用协议的方法,那是糟糕的执行所以编译器或 Xcode 说嘿,你将把该方法声明为常规方法,而不是嵌套方法方式。
//Bridge
protocol xDelegate {
func x(for headerCell: HeaderCell)
}
//This is the class that need the data
//class HeaderCell: UICollectionViewCell
var xDelegate: xDelegate?
//it's init()
override init(frame: CGRect) {
super.init(frame: frame)
let sally = WhereDataIs()
self.xDelegate = sally
xDelegate?.x(for: self)
}
// This extension is added at the end of the class WhereDataIs()
// Inside of this class is it's delegate.
var xDelegate: xDelegate? = nil
extension WhereDataIs: xDelegate {
func x(for headerCell: HeaderCell) {
//Smith value will work because it's dummy
headerCell.lbl.text = "Smith"
// What I really need is instead of "Smith" is the value of didselectItemAt called bingo.
headerCell.lbl.text = bingo
}
}
欢迎任何愿意在这方面指导我的人。
我认为您可以将其添加到 didSelectItemAt indexPath
中当前代码的末尾:
guard let header = collectionView.supplementaryView(forElementKind: "yourElementKind", at: indexPath) as? HeaderCell else { return }
header.lbl.text = bingo
collectionView.reloadData()
编辑:暂时保留其他所有内容,以确保您首先获得好的结果。
让我知道这是否有效,很高兴回来查看。
不使用委托,但它会起作用
解决者:
1) 转到collectionView的控制器。创建一个新变量来存储项目。
// Pass the item object into the xController so that it can be accessed later.
//(didSelectItemAt)
xController.newVar = item
//xController class: UICollectionView...
// MARK: - Properties
var newVar: Thing?
最后,在那个 class 中,您应该有方法 viewForSupplementaryElementOfKind,您可以在其中注册 HeaderCell,然后只需添加...
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! HeaderCell
header.lbl.text = newVar.whatEverYourDataIs
有点笼统,但很管用。 :)