【发布时间】:2019-05-22 18:52:58
【问题描述】:
使用协议/委托并从 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
}
}
向任何想在这方面指导我的人致敬。
【问题讨论】:
-
不鼓励您操作
didSelectRow中的单元格。下次用户滚动时,该值将被还原。更新模型并重新加载行或部分。 -
谢谢,你的意思是我需要将数据传递给那个单元格的父级,这将是点击的项目......但我在那里遇到了一个问题,因为我只是添加一个 collectionView 作为视图,而不是通过任何 NavBar 或 Modal。 '正在以编程方式执行此操作而不使用 IB
标签: ios swift uicollectionview delegates protocols