2015-08-14 44 views
0

我正在嘗試製作TableView的collectionView,以便每個表都包含一個「團隊」的玩家。 (我們的想法是,Players按照他們的等級進行排序,這樣每個球隊都會在最後完成比賽。無論如何)我在選擇合適的球員時遇到了一些麻煩。排序/團隊的代碼工作正常,但我無法弄清楚如何引用tableView dataSource方法中每個表所在的單元格,我需要使用它們各自的玩家填充正確的表格。如果你感到困惑,也許下面的代碼會澄清什麼,我試圖做嵌套在CollectionViewCell中的TableView

class myCollectionViewController: UICollectionViewController, UITableViewDelegate, UITableViewDataSource { 

    //MARK: Properties 

    //These value are passed by the previous ViewController in PrepareForSegue 
    var numberOfTeams = Int?() //2 
    var team = Array<Array<Players>>() //The 2D array of Players which contains the already sorted teams 

    //for right now this is my temporary solution. See below in TableView Data Source 
    var collectionIndex = 0 

    //MARK: UICollectionViewDataSource 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     if numberOfTeams != nil 
      {return numberOfTeams!} 
     else 
      {return 2} 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! myCollectionViewCell 

     cell.teamTable.dataSource = self 
     cell.teamTable.delegate = self 
     //teamTable is a UITableView and is set as an Outlet in the CollectionViewCell. Here I'm just setting it 

     return cell 
    } 

    //MARK: TableView DataSource 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     //add correct amount of rows 
     return team[0].count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     //reset for refresh so index won't go out of bounds of teams Array 
     if collectionIndex == numberOfTeams 
      {collectionIndex = 0 
      print("reset") 
     } 

(這(只以上)是在哪裏失敗,該視圖會正確加載,表格填充他們應該。但如果您滾動到表中的一個底部,這個方法會被調用,並刷新了錯誤的數據表時,你再向上滾動。下面的代碼續)

 let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! teamTableViewCell 
     cell.playerName.text = team[collectionIndex][indexPath.row].name 
     //playerName is a UILabel set as an outlet in the teamTableViewCell class. also, The class Player has a property "name" 

     //once all players are cycled through, add 1 to collectionIndex 
     if team[collectionIndex][indexPath.row] === team[collectionIndex].last 
      {collectionIndex += 1} 

     return cell 
    } 
} 

如上括號說明,目前的實現存在問題,只是一個臨時解決方案,所以我可以繼續研究代碼的其他方面。我需要的是引用collectionViewCell中的tableView是,改變線

cell.playerName.text = team[collectionIndex][indexPath.row].name

cell.playerName.text = team[--"colletionViewCellIndexHere"--][indexPath.row].name

,但我不知道如何引用收集細胞indexPath.item或從tableview: CellForRowAtIndexPath:方法

我嘗試與tableView.superView鬼混,但沒有運氣。任何幫助將不勝感激。在此先感謝

回答

1

更好的方法來做你想做的事情實際上負責管理每個表視圖到一個單獨的視圖控制器,TeamViewController。每個TeamViewController擁有自己的team屬性,一系列玩家。創建集合視圖單元格時,請將新的TeamViewController作爲子視圖控制器添加,並將其視圖添加爲單元格的子視圖。請不要忘記關於細胞重複使用 - 您只需要爲每個細胞添加一個TeamViewController

這樣做會提供一個更好的邏輯分離,並將使管理您的表視圖更簡單。

編輯:

與電池再利用適當的交易的例子:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TeamsCell 
    embedTeamInTeamsCell(cell, atIndexPath: indexPath) 
} 

func embedTeamInTeamsCell(cell: TeamsCell, atIndexPath indexPath: NSIndexPath) { 
    if cell.teamViewController?.team == teams[indexPath.item] { 
     // The cell already has a team view controller, but it's for the wrong team – remove it 
     detachTeamFromTeamsCell(cell) 
    } 

    if cell.teamViewController == nil { 
     // The cell doesn't currently have a team view controller – create a new one and add it 
     let teamVC = self.storyboard?.instantiateViewControllerWithIdentifier("TeamViewController") as! TeamViewController 
     addChildViewController(teamVC) 
     cell.addSubview(teamVC.view) 
     // ... setup constraints on the newly added subview, or set frame 
     teamVC.didMoveToParentViewController(self) 
     cell.teamViewController = teamVC 
    } 
} 

func detachTeamFromTeamsCell(cell: TeamsCell) { 
    if let teamVC = cell.teamViewController { 
     teamVC.willMoveToParentViewController(nil) 
     cell.teamViewController = nil 
     teamVC.view.removeFromSuperview() 
     teamVC.removeFromParentViewController() 
    } 
} 

在現實中,它很可能是最好只當重用當前的嵌入式TeamViewController實例,而不是丟棄它們完全您需要更改單元格中的嵌入式團隊。然後,只需改變對TeamViewControllerteam屬性應該是足夠的重新配置視圖控制器來顯示新的團隊(也許你只需要調用tableView.reloadDatateamdidSet財產觀察員 - 取決於你如何構造你的類):

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TeamsCell 
    if cell.teamViewController == nil { 
     // The cell doesn't currently have a team view controller – create a new one and add it 
     let teamVC = self.storyboard?.instantiateViewControllerWithIdentifier("TeamViewController") as! TeamViewController 
     addChildViewController(teamVC) 
     cell.addSubview(teamVC.view) 
     // ... setup constraints on the newly added subview, or set frame 
     teamVC.didMoveToParentViewController(self) 
     cell.teamViewController = teamVC 
    } 

    cell.teamViewController!.team = teams[indexPath.item] 
} 


class TeamViewController: UITableViewController { 
    //... 

    var team: Team? { 
     didSet { tableView.reloadData() } 
    } 
} 
+0

聽起來像一個計劃。我是一個快速編程的新手,有沒有什麼特別的事情需要我去設置這個新的'TeamViewController'作爲「子視圖控制器」?一個創建子TeamViewController的小語法示例肯定會有幫助。 (我認爲'collectionView:numberOfItemsInSection'將是添加'TeamViewControllers'的最佳位置,因爲(據我所知)在設置視圖時只調用一次) –

+1

當然,你需要'UIViewController'上的一些包含方法在添加/刪除子視圖控制器時調用。我在[另一個答案](http://stackoverflow.com/a/31959445/429427)中寫了一個簡短的例子,否則你可以在[UIViewController'文檔](https://developer.apple)中查看遏制方法.COM /庫/ IOS /文檔/ UIKit的/參考/ UIViewController_Class /#// apple_ref/OCC/instm/UIViewController中/ addChildViewController :)。 – Stuart

+0

我會努力的。看起來有點進步,因爲我很快就沒有一個月快速學習,但不應該太難實施。 Thanx的幫助和編輯 –