gpt4 book ai didi

ios - 从另一个 View Controller 返回时 UICollectionView 重复单元格

转载 作者:行者123 更新时间:2023-11-30 12:14:16 24 4
gpt4 key购买 nike

我一直在开发天气应用程序并使用 UICollectionView 来显示天气数据。

每当我打开另一个 View Controller 并返回到 UICollectionView 的 View Controller 时,我都会得到重复的单元格。

这是代码。我使用 Alamofire 发出 api 请求,将 json 结果附加到本地字符串,然后将其分配给单元格的文本标签。

    class VaanizhaiViewController: UICollectionViewController {


// MARK: - flow layout

let columns : CGFloat = 2.0
let inset : CGFloat = 3.0
let spacing : CGFloat = 3.0
let lineSpacing : CGFloat = 3.0
var isRandom : Bool = false

// MARK: - Variables
var apiKey: String = "55742b737e883a939913f2c90ee11ec0"
var country : String = ""
var zipCode : String = ""
var json : JSON = JSON.null
var cityNames: [String] = []
var temperature: [Int] = []
var weather: [String] = []

// MARK: - Actions


// MARK: - view did load
override func viewDidLoad() {
super.viewDidLoad()
let layout = BouncyLayout()
self.collectionView?.setCollectionViewLayout(layout, animated: true)
self.collectionView?.reloadData()

}

override func viewDidAppear(_ animated: Bool) {
for i in 0...zip.count-1{
parseURL(zipCode: "\(zip[i])", country: "us")
}

}


// MARK: - Functions

func parseURL(zipCode: String, country: String){

let url = "http://api.openweathermap.org/data/2.5/weather?zip=\(zipCode),\(country)&APPID=\(apiKey)&units=metric"
requestWeatherData(link: url)
}

func requestWeatherData(link: String){
Alamofire.request(link).responseJSON{ response in
if let value = response.result.value{
self.json = JSON(value)
self.cityNames.append(self.json["name"].string!)
let cTemp = ((self.json["main"]["temp"].double)!)
self.temperature.append(Int(cTemp))
let cWeather = self.json["weather"][0]["main"].string!
self.weather.append(cWeather)
self.collectionView?.reloadData()

}
}

}




// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {

return 1
}


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

return self.cityNames.count

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "weatherCell", for: indexPath) as! WeatherViewCell

if !self.cityNames.isEmpty {


cell.cityLabel.text = self.cityNames[indexPath.row]
cell.tempLabel.text = String (self.temperature[indexPath.row])
cell.weatherLabel.text = self.weather[indexPath.row]
cell.backgroundColor = UIColor.brown
}

return cell


}
// MARK: - UICollectionViewDelegateFlowLayout

extension VaanizhaiViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = Int ((CGFloat(collectionView.frame.width) / columns) - (inset + spacing))
return CGSize(width: width, height: width)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: CGFloat(inset), left: CGFloat(inset), bottom: CGFloat(inset), right: CGFloat(inset))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return spacing
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return lineSpacing
}
}

最佳答案

每次访问 View Controller 时都会调用 viewDidAppear,而 viewDidLoad 仅在创建时被调用一次。

因此,您应该将对 self.collectionView?.reloadData() 的调用切换到 viewDidAppear,将对 parseURL 的调用切换到 viewDidLoad

如果您需要更新天气数据(例如,刷新时),那么您需要重构 requestWeatherData 以停止附加到数组,并进行替换。

关于ios - 从另一个 View Controller 返回时 UICollectionView 重复单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624632/

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