我一直在研究天气应用,并一直使用UICollectionView来显示天气数据。
每当我打开另一个视图控制器并返回到UICollectionView的View控制器时,我都会得到重复的单元格。
这是代码。 我使用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
}
}