我希望成为展示对象的一部分,该对象是国家countryID
的一部分,与对象一致。
在不同的城市,有对象
我想表明Idi的城市等于countryID
。
for key in self.cityId{
if key == self.countryID{
self.countCity++
}
}
但是当我运行return console
时
和模拟器
代码:
写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)
}
你认为这是问题吗?
答案 0 :(得分: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
}
答案 1 :(得分: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)
}
}
并返回真实数据:)