2016-02-26 123 views
-1

我想成爲顯示對象的一部分,國家的一部分countryID,這與對象一致。 在不同的城市,有對象 我想表明,與Idi的城市等於‍‍countryID在tableview中過濾數據

for key in self.cityId{ 
      if key == self.countryID{ 
       self.countCity++ 
      } 
     } 

但是當我運行控制檯回報

enter image description here

和模擬器

enter image description here

代碼:

寫numberOfRowsInSection

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     for key in self.cityId{ 
      if key == self.countryID{ 
       self.countCity++ 
      } 
     } 

     return countCity 
    } 

和的cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cityCell", forIndexPath: indexPath) 
     let city = cityArray[indexPath.row] 
     if city.cityId == countryID{ 
     if let NameCity:String = city.nameCity { 


      cell.textLabel?.text = NameCity 
      print(NameCity) 
      } 
     } 

     // Configure the cell... 

     return cell 
    } 

解析API

func GetPassCity(){ 

     NSURLSession.sharedSession().dataTaskWithURL(urlCity){[unowned self] (data ,response ,error) in 
      if error != nil{ 
       print("A") 
       print(error!) 
      }else{ 


       do{ 
       //readin data from Server 
        let posts = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]] 
        //save data 
        for post in posts{ 
         var postCity:City? 
         if let idCity = post["userId"] as? Int , let nameCity = post["title"] as? String{ 
          postCity = City(idCity: idCity, nameCity: nameCity) 

          self.cityId.append(idCity) 

         } 

         self.cityArray.append(postCity!) 

         dispatch_async(dispatch_get_main_queue()){ 

          print(self.countCity) 
          self.tableView.reloadData() 
         } 


        } 
       }catch let error as NSError{ 

        print("B") 
        print(error) 
       } 
      } 


     }.resume() 

     print(cityArray) 
    } 

你認爲是什麼問題?

+1

'self.countCity + = 1'或之前,我已經這樣做了'self.countCity ++' – boidkan

+0

。但它是錯誤'預期的表達式後運算符' – phantom

+0

然後嘗試解包countCity因爲'self.countCity!++'只是確保它被初始化,而不是零 – boidkan

回答

1

您想要self.countCity += 1,self.countCity!++++self.countCity!。做self.countCity = +1只是將countCity設置爲1.打開一個遊樂場並嘗試一下。

所以:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    for key in self.cityId{ 
     if key == self.countryID{ 
      self.countCity!++ 
     } 
    } 

    return countCity 
} 
+0

謝謝。它是正確的。但問題沒有解決。更新代碼 – phantom

0

我發現溶液白衣用濾波器()

代碼: 寫入numberOfRowsInSection

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 

    return citySelected.count 
} 

和的cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cityCell", forIndexPath: indexPath) 
    let city = citySelected[indexPath.row] 

    if let NameCity:String = city.nameCity { 


     cell.textLabel?.text = NameCity 
     print(NameCity) 

    } 

    // Configure the cell... 

    return cell 
} 

解析API

FUNC GetPassCity(){

NSURLSession.sharedSession().dataTaskWithURL(urlCity){[unowned self] (data ,response ,error) in 
     if error != nil{ 
      print("A") 
      print(error!) 
     }else{ 


      do{ 
      //readin data from Server 
       let posts = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]] 
       //save data 
       for post in posts{ 
        var postCity:City? 
        if let idCity = post["userId"] as? Int , let nameCity = post["title"] as? String{ 
         postCity = City(idCity: idCity, nameCity: nameCity) 

         self.cityId.append(idCity) 

        } 

        self.cityArray.append(postCity!) 



let filter = self.cityArray.filter({ $0.cityId == self.countryID}) 
        self.citySelected = filter 


        dispatch_async(dispatch_get_main_queue()){ 

         print(self.countCity) 
         self.tableView.reloadData() 
        } 


       } 
      }catch let error as NSError{ 

       print("B") 
       print(error) 
      } 
     } 

,並返回真實的數據:)你想