问题:下面提到的代码为它应该填充在相关部分标题下的两个部分填充了值(部分即:事件和已完成)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MainTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MainTableViewCell
DispatchQueue.main.async{
let theGame = self.game[indexPath.row]
if (theGame.status == "Finished" )
{
let finishedGame = theGame
cell.userName?.text = finishedGame.myTeamState.team.players[1].username
}
else{
let otherGame = theGame
cell.userName?.text = otherGame.myTeamState.team.players[1].username
}
}
return cell
}
我认为,我无法理解如何让 header 部分在其中嵌套值/填充列表。
提前致谢。
最佳答案
首先从您的游戏数组创建两个数组:
var finishedArray = []
var activeArray = []
for item in game {
if item.status == "Finished" {
finishedArray.append(item)
}else {
activeArray.append(item)
}
}
tableView.reloadData
在此之后,使用 numberOfRowsInSection 方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return finishedArray.count
}else {
return activeArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MainTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MainTableViewCell
if (indexPath.section == 0) {
let theGame = self.finishedArray[indexPath.row]
let finishedGame = theGame
cell.userName?.text = finishedGame.myTeamState.team.players[1].username
}
else{
let theGame = self.activeArray[indexPath.row]
let otherGame = theGame
cell.userName?.text = otherGame.myTeamState.team.players[1].username
}
return cell
}
您还需要添加 numberOfSections 和 numberOfRowsInSection 方法。
关于ios - 在 Swift 3 中相应部分下的 UiTableView 中显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968876/