2017-05-05 38 views
0

我想從另一個swift文件使用CollectionView方法,而不是它的ViewController出於某種原因。從另一個swift文件使用CollectionView方法

我有這個在我的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這不是問題的一部分。

+0

請確保collectionView的Outlets已正確連接,並且代理和數據源未從Storyboard附加。 –

+0

是的,是的,我已經檢查過它,委託和數據源沒有連接到任何信息,所以當我用'viewDidLoad'方法覆蓋它時,它應該是我寫的類。 –

+0

您可以在分配數據源和委託給collection後檢查collectionView.delegate和collectionView.dataSource的值是什麼。 –

回答

0

設置數據源,並委託在viewDidLoad中()方法集合視圖後,重新加載集合視圖:

collectionView.reloadData() 
0

這所創建被稱爲CustomDataSource另一個類,你可以找到一個教程here

嘗試在設置dataSource和委託後調用 collectionView.reloadData()

並確保eventItems不爲空。

希望它能幫助!

0

除了在你的情況下無法正常工作的問題,這可能是由於沒有配置正確的集合視圖,可以使用從其他用戶上面得到的答案來解決,

沒有爲你的「的問題的方法使用來自另一個迅速文件」
你可以利用所謂的概念‘擴展’的CollectionView方法,我將解釋如何。

  1. 用於處理集合視圖方法如下創建一個新類,
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 
    } 

} 
  • 而在的viewController文件分配爲集合視圖數據源&委託方法如下,
  • 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. 更新的數據類型&集合視圖屬性,如小區標識根據自己的需要。