【发布时间】:2015-08-14 22:16:25
【问题描述】:
我正在尝试制作 TableViews 的 collectionView,以便每张桌子都包含一个“团队”的玩家。 (这个想法是Players 按他们的评分同样排序,这样每支球队都会在最后。无论如何)我在用合适的球员填充桌子时遇到了一些麻烦。排序/团队制作代码工作正常,但我不知道如何从 tableView dataSource 方法中引用每个表所在的单元格,我需要用它们各自的玩家填充正确的表。如果您感到困惑,也许下面的代码会阐明我要做什么
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
}
}
如上面括号中所述,当前的实现存在问题,只是临时解决方案,因此我可以继续处理代码的其他方面。我需要的是引用tableView所在的collectionViewCell并更改行
cell.playerName.text = team[collectionIndex][indexPath.row].name
到
cell.playerName.text = team[--"colletionViewCellIndexHere"--][indexPath.row].name
但我不知道如何在tableview: CellForRowAtIndexPath: 方法中引用集合单元格 indexPath.item 或行
我尝试与tableView.superView 混在一起,但没有运气。任何帮助将不胜感激。提前致谢
【问题讨论】:
标签: ios arrays swift uitableview uicollectionview