2017-08-10 85 views
0

我一直在使用天氣應用程序,並且一直在使用UICollectionView來顯示天氣數據。從另一個視圖控制器返回時,UICollectionView複製單元格

每當我打開另一個視圖控制器並返回到UICollectionView的視圖控制器,我得到重複的單元格。

這是代碼。 我使用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 
    } 
} 

回答

0

你viewDidAppear將被調用每次訪問視圖控制器的時間,它的創建,當你的viewDidLoad只會被調用一次。

因此,你應該您的通話切換到self.collectionView?.reloadData()到viewDidAppear和您的來電的parseURL到viewDidLoad中

,如果你需要更新您的天氣數據(在刷新,例如),那麼你需要重新構造你的requestWeatherData來停止附加到你的數組,並替換。

相關問題