使用另一个 swift 文件中的 CollectionView 方法 [英] Use CollectionView methods from another swift file

查看:24
本文介绍了使用另一个 swift 文件中的 CollectionView 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我想使用另一个 swift 文件中的 CollectionView 方法,而不是它的 ViewController.

我的 ViewController 中有这个:

@IBOutlet 弱变量 collectionView:UICollectionView!var broadcastCollectionView = BroadcastCollectionView()覆盖 func viewDidLoad() {super.viewDidLoad()broadcastColletionView = BroadcastCollectionView(eventItems: eventItems,collectionView: collectionView, BroadastObject: BroadastObject)collectionView.dataSource = broadcastColletionViewcollectionView.delegate = broadcastColletionView}

我有包含 CollectionView 委托方法的 BroadcastCollectionView.swift :

class BroadcastCollectionView: NSObject,UICollectionViewDelegate, UICollectionViewDataSource {var eventItems = [事件类型]()var alreadyChecked: Bool = falsevar cellHistory: IndexPath = []var collectionView:UICollectionView!var BroadastObject = Broadcast()init(eventItems: [Eventtype],collectionView: UICollectionView,广播对象:广播){self.eventItems = eventItemsself.collectionView = collectionViewself.broadastObject = BroadastObject}func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{返回 eventItems.count}func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->UICollectionViewCell {let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "brCollectionView", for: indexPath) as!BroadcastCollectionViewCellself.collectionView = collectionViewcell.eventImage.image = eventItems[indexPath.row].imagecell.eventType = eventItems[indexPath.row]让 tap = UITapGestureRecognizer(target: self, action: #selector(collectionViewTapped))tap.numberOfTapsRequired = 1cell.addGestureRecognizer(点击)返回单元格}@objc func collectionViewTapped(sender: UITapGestureRecognizer) {如果让 indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {让单元格:BroadcastCollectionViewCell = collectionView.cellForItem(at: indexPath)!作为!BroadcastCollectionViewCell打印(项目索引")} 别的 {print("集合视图被点击")}}func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {打印(选定的行是",indexPath.row)}

如果我将 collectionView.delegate 和 dataSource 设置为 BroadcastCollactionView 类,我真的不明白为什么不调用委托方法.请不要让我解释为什么我要分开这个 CollectionView 这不是问题的一部分.

解决方案

除了在您的情况下无法正常工作的问题外,这可能是由于未正确配置集合视图而导致的,这可以使用您在上面从其他人那里得到的答案解决用户,

有一种方法可以解决使用另一个 swift 文件中的 CollectionView 方法"的问题
您可以使用称为扩展"的概念,我将向您解释如何使用.

  1. 创建一个用于处理集合视图方法的新类,如下所示,

<块引用>

class MyDataSource: NSObject {var eventItems: Array?初始化(事件:数组<任何>?){self.eventItems = 事件}}扩展 MyDataSource: UITableViewDataSource, UITableViewDelegate {//MARK: - 表视图数据源方法func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier")返回单元格}//添加额外的数据源方法 &根据您的需要委托方法//MARK: - 表视图委托方法func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {//执行你的操作//使用委托方法(协议)从 viewController 类中执行您的操作}}

  1. 并在 viewController 文件中分配数据源 &集合视图的委托方法如下,

<块引用>

类视图控制器:UIViewController {var 数据源:MyDataSource?

override func viewDidLoad() {super.viewDidLoad()//初始化数据源 &带有扩展数据源的 collectionView 委托数据源 = MyDataSource(事件:EVENTITEMS_ARRAY)collectionView?.dataSource = 数据源collectionView?.delegate = 数据源}}

注意

  1. 更新数据类型 &集合视图属性,如根据您需要的单元格标识符.

I'd like to use a CollectionView methods from another swift file instead of it's ViewController for some reason.

I have this in my 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
}

And I have BroadcastCollectionView.swift which contains the CollectionView delegate methods:

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)
}

I don't really understand why the delegate methods not called if I setted the collectionView.delegate and dataSource to the BroadcastCollactionViewclass. Please don't make me explain why would I like to separate this CollectionView it's not part of the question.

解决方案

Apart from the issue of not working in your case which could be due to not configuring the collection view properly which can be resolved using the answers you got above from other users,

There is an approach for your question of "Use CollectionView methods from another swift file"
You can make use of the concept called "extension", I will explain you how.

  1. Create a new class for handling the collection view methods as follows,

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
    }

}

  1. And in the viewController file assign the datasource & delegate method for collection view as follows,

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
    } 
}

NOTE

  1. Update the data types & collection view properties like cell identifier as per your need.

这篇关于使用另一个 swift 文件中的 CollectionView 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆