2016-02-26 58 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) 
      } 
     } 

,并返回真实的数据:)你想