如何在 UICollectionViewLayout 和 UIViewController 之间传递数据 [英] how to pass data between UICollectionViewLayout and UIViewController
问题描述
我需要基于对 api 的 http 调用创建自定义布局.根据循环中 api 的结果,我将从 CollectionView 填充一个数组
在 CustomLayout 中,我需要使用这个数组才能在每一行中正确绘制细节.在 collectionView 中,我在将数据添加到数组后使用重新加载数据
谢谢你
代码:
class epgViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{var 事件 = [事件]()@IBOutlet 弱变量 collectionView:UICollectionView!//另一个初始化...//====== 加载事件 ==============func get_events() {//http api调用//作为循环的结果,我将数据附加到 Events()self.events.append(newEvent)//重新加载集合视图数据self.collectionView.reloadData()}}/////////////布局////////类 customCollectionViewLayout:UICollectionViewLayout {//准备布局覆盖 func prepareLayout() {对于 0...collectionView 中的部分!.numberOfSections()-1 {如何从 collectionView 访问数组事件[i] ????}}}
我不完全确定我了解您的情况,但我会提供两种在您的应用程序中进行通信的方法.
使用 NSNotificationCenter
在需要触发事件的控制器的viewDidLoad中添加一个观察者:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "remoteRefresh:", name:"remoteRefreshID", object: nil)
在同一个控制器中添加一个与我们的选择器名称相同的观察者,在上面的例子中是remoteRefresh".
func remoteRefresh(notification: NSNotification) {//做东西}
然后在您的另一个控制器中可以发布消息并且您的 remoteRefresh 操作将运行:
NSNotificationCenter.defaultCenter().postNotificationName("remoteRefreshID", object: nil)
有关更多详细信息,我有一个关于 NSNotificationCenter 的两部分教程
协议/委托模式
第二个是协议/委托模式,它有点复杂,但我在这里有一个书面教程:
另一种选择
如果您仍有问题,这里有一个选项.您可以声明一个全局结构来保存您的数据,然后将其粘贴到 Playground 中以进行理解.
struct SessionData {静态 var 事件 = [String]()}类 classOne {函数添加值(){SessionData.events.append("一条消息")SessionData.events.append("另一条消息")}}类类二{func getValues() ->[细绳] {返回 SessionData.events}}classOne().addValues()classTwo().getValues()
I need to create a custom layout based on http call to an api. Based on result from api in loop i will populate an array from CollectionView
In CustomLayout I need to use this array in order to draw correctly details in each row. In collectionView I use reload data after the data was added to array
Thank YOu
code:
class epgViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
var events = [Event]()
@IBOutlet weak var collectionView: UICollectionView!
//another initializations
. . .
//====== load Events ===============
func get_events() {
//http api call
//as result in loop i append data to Events()
self.events.append(newEvent)
//reload collection view data
self.collectionView.reloadData()
}
}
//////////
/// LAyout
////////
class customCollectionViewLayout: UICollectionViewLayout {
//prepare layout
override func prepareLayout() {
for section in 0...collectionView!.numberOfSections()-1 {
how to access array from collectionView
Events[i] ????
}
}
}
I'm not entirely sure I understand your situation, but I'll provide two methods for communication within your application.
Using NSNotificationCenter
Add an observer In the viewDidLoad of the controller that needs an event fired:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "remoteRefresh:", name:"remoteRefreshID", object: nil)
In that same controller add an observer with a name the same as our selector, in the above example "remoteRefresh".
func remoteRefresh(notification: NSNotification) {
// do stuff
}
Then in your other controller can post messages and your remoteRefresh action will run:
NSNotificationCenter.defaultCenter().postNotificationName("remoteRefreshID", object: nil)
for more details I have a two part tutorial on NSNotificationCenter
Protocol / Delegate pattern
The second is the Protocol / Delegate pattern which is a bit more involved but I have a written tutorial here:
Another Option
Here's an option if you're still having issues. You could declare a global struct to hold your data, paste this into a playground to understand.
struct SessionData {
static var events = [String]()
}
class classOne {
func addValues() {
SessionData.events.append("a message")
SessionData.events.append("another message")
}
}
class classTwo {
func getValues() -> [String] {
return SessionData.events
}
}
classOne().addValues()
classTwo().getValues()
这篇关于如何在 UICollectionViewLayout 和 UIViewController 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!