gpt4 book ai didi

ios - 为 indexPath 获取 didSelectItemAt 上的值并将其添加到委托(delegate)/协议(protocol)以填充标题单元格

转载 作者:可可西里 更新时间:2023-11-01 01:49:05 27 4
gpt4 key购买 nike

使用协议(protocol)/委托(delegate)并从 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)

那个宾果游戏正在打印我需要的值。那里非常好。

现在,我不能在那个函数内部使用协议(protocol)的方法,那是糟糕的执行所以编译器或 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
}
}

对于任何愿意在这方面指导我的人,我们都将脱帽致敬。

最佳答案

不使用委托(delegate),但它会起作用

解决者:

1) 转到 collectionView 的 Controller 。创建一个新变量来存储项目。

// 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?

最后在该类中,您应该拥有方法 viewForSupplementaryElementOfKind,您可以在其中注册 HeaderCell,然后只需添加...

let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! HeaderCell

header.lbl.text = newVar.whatEverYourDataIs

有点通用,但很管用。 :)

关于ios - 为 indexPath 获取 didSelectItemAt 上的值并将其添加到委托(delegate)/协议(protocol)以填充标题单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56263242/

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