gpt4 book ai didi

ios - 在 Swift 3 中相应部分下的 UiTableView 中显示列表

转载 作者:行者123 更新时间:2023-11-29 00:34:24 25 4
gpt4 key购买 nike

问题:下面提到的代码为它应该填充在相关部分标题下的两个部分填充了值(部分即:事件和已完成)

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com