gpt4 book ai didi

arrays - TableViewCell 重复显示数组中的最后一项

转载 作者:行者123 更新时间:2023-11-28 11:28:47 25 4
gpt4 key购买 nike

我创建了一个从 JSON 文件填充的数组。当我尝试用数组的项目填充 TableView 时,即使 indexPath.row 正确返回,它也会重复显示最后一条记录。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return leagueStandingsModelArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTeam", for: indexPath)

let team = self.leagueStandingsModelArray[indexPath.row]

cell.textLabel?.text = String(team.teamId)
print(team.teamName)
print(indexPath.row)

return cell
}

在我的调试窗口中,这是我得到的结果:

打印了我的数组项

1 -- Santos -- 32

2 -- Palmeiras -- 28

3 -- Flamengo -- 24

4 -- Atletico-MG -- 24

这是我从下面这两行得到的返回

print(team.teamName)

print(indexPath.row)

Atletico-MG

0

Atletico-MG

1

Atletico-MG

2

Atletico-MG

3

应该打印前四支球队而不是第四支球队。

完整代码

导入 UIKit进口 Alamofire导入 SwiftyJSON

LeagueOverviewViewController 类:UIViewController、UITableViewDataSource、UITableViewDelegate {

let leagueModel = LeagueIdModel()
var leagueParams = LeagueParametersModel()
var leagueStandingsModelArray = [LeagueStandingsModel]()

var urls = URLs()

@IBOutlet weak var standingsTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return leagueStandingsModelArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTeam", for: indexPath)

let team = self.leagueStandingsModelArray[indexPath.row]

cell.textLabel?.text = String(team.teamId)
print(leagueStandingsModelArray[0].teamName)
print(leagueStandingsModelArray[1].teamName)
print(leagueStandingsModelArray[2].teamName)

return cell
}

override func viewDidLoad() {
super.viewDidLoad()

getLeagueIdJSON(country: leagueParams.country, season: leagueParams.season)


standingsTableView.delegate = self
standingsTableView.dataSource = self
}

//GET THE JSON FILE TO RETRIEVE LEAGUE ID USING COUNTRY AND SEASON AS PARAMETERS
func getLeagueIdJSON (country: String, season: Int) {

let headers = Headers().getHeaders()
let url = urls.getLeagueUrlByCountryAndSeason(country: leagueParams.country, season: leagueParams.season)

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseJSON {
response in
if response.result.isSuccess {
let leagueJSON = JSON(response.result.value!)

self.getLeagueId(json: leagueJSON, country: self.leagueParams.country, season: self.leagueParams.season)
self.getLeagueStandings(leagueId: self.leagueModel.leagueId)

}
}
}


//GET LEAGUE_ID FROM JSON FILE RETRIEVED
func getLeagueId(json: JSON, country: String, season: Int) {

var leagueId = 0

if country == "gb" {

for league in json["api"]["leagues"].arrayValue {
if league["name"].stringValue == "Premier League" {
leagueId = league["league_id"].intValue
}
}
}
else if country == "br" {

for league in json["api"]["leagues"].arrayValue {
if league["name"].stringValue == "Serie A" {
leagueId = league["league_id"].intValue
}
}
}

leagueModel.leagueId = leagueId
}

//GET LEAGUE STANDINGS FROM A LEAGUE ID (TOP 4)
func getLeagueStandings (leagueId: Int) {

let headers = Headers().getHeaders()
let url = urls.getLeagueStandingsUrl(leagueId: leagueId)
let leagueStandingsModel = LeagueStandingsModel()
var counter = 0

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseJSON {
response in
if response.result.isSuccess {
let standingsJSON = JSON(response.result.value!)

for standings in standingsJSON["api"]["standings"][counter].arrayValue {
if counter <= 3 {
leagueStandingsModel.teamId = standings["team_id"].intValue
leagueStandingsModel.rank = standings["rank"].intValue
leagueStandingsModel.teamName = standings["teamName"].stringValue
leagueStandingsModel.points = standings["points"].intValue
leagueStandingsModel.matchesPlayed = standings["all"]["matchsPlayed"].intValue
leagueStandingsModel.win = standings["all"]["win"].intValue
leagueStandingsModel.draw = standings["all"]["draw"].intValue
leagueStandingsModel.lose = standings["all"]["lose"].intValue
leagueStandingsModel.goalsFor = standings["all"]["goalsFor"].intValue
leagueStandingsModel.goalsAgainst = standings["all"]["goalsAgainst"].intValue

self.leagueStandingsModelArray.append(leagueStandingsModel)

let standings = String(self.leagueStandingsModelArray[counter].rank) + " -- " +
self.leagueStandingsModelArray[counter].teamName + " -- " +
String(self.leagueStandingsModelArray[counter].points)

print(standings)

counter += 1
}
}
}
self.standingsTableView.reloadData()
}
}

最佳答案

不是重用 让 leagueStandingsModel = LeagueStandingsModel() 在填充它之前创建新对象:

if counter <= 3 {
var leagueStandingsModel = LeagueStandingsModel()
leagueStandingsModel.teamId = standings["team_id"].intValue
...

关于arrays - TableViewCell 重复显示数组中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57359383/

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