我希望使用其他swift文件中的CollectionView
方法而不是ViewController
出于某种原因。
我在ViewController
:
@IBOutlet weak var collectionView: UICollectionView!
var broadcastColletionView = BroadcastCollectionView()
override func viewDidLoad() {
super.viewDidLoad()
broadcastColletionView = BroadcastCollectionView(eventItems: eventItems,collectionView: collectionView, broadastObject: broadastObject)
collectionView.dataSource = broadcastColletionView
collectionView.delegate = broadcastColletionView
}
我有BroadcastCollectionView.swift,它包含CollectionView委托方法:
class BroadcastCollectionView: NSObject,UICollectionViewDelegate, UICollectionViewDataSource {
var eventItems = [Eventtype]()
var alreadyChecked: Bool = false
var cellHistory: IndexPath = []
var collectionView: UICollectionView!
var broadastObject = Broadcast()
init(eventItems: [Eventtype],collectionView: UICollectionView,
broadastObject: Broadcast) {
self.eventItems = eventItems
self.collectionView = collectionView
self.broadastObject = broadastObject
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return eventItems.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "brCollectionView", for: indexPath) as! BroadcastCollectionViewCell
self.collectionView = collectionView
cell.eventImage.image = eventItems[indexPath.row].image
cell.eventType = eventItems[indexPath.row]
let tap = UITapGestureRecognizer(target: self, action: #selector(collectionViewTapped))
tap.numberOfTapsRequired = 1
cell.addGestureRecognizer(tap)
return cell
}
@objc func collectionViewTapped(sender: UITapGestureRecognizer) {
if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
let cell : BroadcastCollectionViewCell = collectionView.cellForItem(at: indexPath)! as! BroadcastCollectionViewCell
print("item index")
} else {
print("collection view was tapped")
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selected row is",indexPath.row)
}
如果我将collectionView.delegate和dataSource设置为BroadcastCollactionView
类,我真的不明白为什么没有调用委托方法。请不要让我解释为什么我要将此CollectionView
分开呢{<1}}这不是问题的一部分。
答案 0 :(得分:1)
除了你的情况下没有工作的问题,可能是因为没有正确配置集合视图,可以使用你从其他用户那里得到的答案来解决,
有一种方法对于你的问题“从另一个swift文件中使用CollectionView方法”
您可以使用名为“扩展”的概念,我将向您解释如何。
class MyDataSource: NSObject { var eventItems: Array<Any>? init(events: Array<Any>?) { self.eventItems = events } } extension MyDataSource: UITableViewDataSource, UITableViewDelegate { // MARK: - Table view data source methods func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier") return cell } // Add additional data source methods & delegate methods as per your need // MARK: - Table view delegate methods func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Perform your action // Use delegate methods(protocols) to do your actions from the viewController class } }
class ViewController:UIViewController { var datasource:MyDataSource?
override func viewDidLoad() { super.viewDidLoad() // Initialize datasource & delegate for collectionView with the extension datasource datasource = MyDataSource(events: EVENTITEMS_ARRAY) collectionView?.dataSource = datasource collectionView?.delegate = datasource } }
注意强>
答案 1 :(得分:0)
在viewDidLoad()方法中为集合视图设置数据源和委托后,重新加载集合视图:
v <- c("ace", "king", "queen", "jack", "joker")
library(zoo)
rollapply(v, width=3, paste, by=2)
# [,1] [,2] [,3]
#[1,] "ace" "king" "queen"
#[2,] "queen" "jack" "joker"
答案 2 :(得分:0)
您创建的另一个类名为CustomDataSource,您可以找到教程here
尝试打电话
设置dataSource和委托后collectionView.reloadData()
。
并确保 eventItems 不为空。
希望它有帮助!