2016-02-11 20 views
0

在我的應用程序中,我有兩個表視圖。第一個表格視圖有一組單元格。這些單元格將永遠是相同的,永遠不會改變上面的表格視圖將始終有4個單元格,而且從不會更多。在我的服務器上,我有我的API,其中每個單元都有路由。在DetailTableView中返回數據(swift)

例如:

GET - myAPI /空氣

GET - myAPI /歷史

GET - myAPI /火車

GET - myAPI /出租車

而且每個路線發回不同的數據

mainTablewView:

import UIKit 
enum NeededAPI { 
case Air 
case History 
case Train 
case Taxi 
} 
class mainTableViewController : UITableViewController { 
struct WeatherSummary { 
    var id: String 
} 

var testArray = NSArray() 
var manuArray = NSArray() 

// Array of sector within our company 
var selectSector: [String] = ["Air", "History","Train","Taxi"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.rowHeight = 80.0 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.selectSector.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("sectorList", forIndexPath: indexPath) 
    // Configure the cell... 

    if selectSector.count > 0 { 

     cell.textLabel?.text = selectSector[indexPath.row] 
    } 

    return cell 
} 


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if (segue.identifier == "AirSegue"){ 
     if let destination = segue.destinationViewController as? AirTableViewController { 
      let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow! 

      if let row:Int = indexPath.row { 

      destination.apiThatNeedsToBeCalled = .Air 

      } 


     } 
    } 
    if (segue.identifier == "HistorySegue"){ 
     if let destination = segue.destinationViewController as? HistoryTableViewController { 
      let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow! 

      if let row:Int = indexPath.row { 

       destination.apiThatNeedsToBeCalled = .History 

      } 


     } 
    } 
    if (segue.identifier == "TrainSgue"){ 
     if let destination = segue.destinationViewController as? TrainTableViewController { 
      let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow! 

      if let row:Int = indexPath.row { 

       destination.apiThatNeedsToBeCalled = .Train 

      } 


     } 
    } 
    if (segue.identifier == "TaxiSegue"){ 
     if let destination = segue.destinationViewController as? TaxiTableViewController { 
      let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow! 

      if let row:Int = indexPath.row { 

       destination.apiThatNeedsToBeCalled = .Taxi 

      } 


     } 
    } 




} 

} 

和郵政

import Foundation 
class Post : CustomStringConvertible { 
var userId:Int 
var title: String 

init(userid:Int , title:String){ 
    self.userId = userid 
    self.title = title 
} 

var description : String { return String(userId) } 

} 

當用戶選擇單元格設置爲apiThatNeedsToBeCalled正確的值。一旦你這樣做了,didSet中的代碼就會被執行,它應該調用調用相應API的函數。

到其他的tableView:

import UIKit 

class AirTableViewController: UITableViewController { 

var postCollection = [Post]() 
     var apiThatNeedsToBeCalled:NeededAPI = .Air { 
     didSet { 
      //check which API is set and call the function which will call the needed API 
      AirLine() 

     } 
    } 

override func viewDidLoad() { 
    super.viewDidLoad() 




} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
var apiThatNeedsToBeCalled:NeededAPI = .Air { 
    didSet { 
     //check which API is set and call the function which will call the needed API 
     AirLine() 

    } 
} 

func AirLine(){ 

    let url = NSURL(string: "http://jsonplaceholder.typicode.com/posts") 
    NSURLSession.sharedSession().dataTaskWithURL(url!){[unowned self] (data , respnse , error) in 
     if error != nil{ 

      print(error!) 
     }else{ 

      do{ 

       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]] 

       UIApplication.sharedApplication().networkActivityIndicatorVisible = false 

       var newPost = Iduser(id: 0) 

       for posts in json { 

        let postObj = Post(userid:posts["userId"] as! Int,title: posts["title"] as! String) 

        self.postCollection.append(postObj) 
       } 

       dispatch_async(dispatch_get_main_queue()){ 
        self.tableView.reloadData() 
       } 
      }catch let error as NSError{ 

       UIApplication.sharedApplication().networkActivityIndicatorVisible = true 
       print(error.localizedDescription) 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON:\(jsonStr)") 

       dispatch_async(dispatch_get_main_queue()) { 

        let alert = UIAlertController(title: "Alert", message: "Oops! Wrong Details, Try Again", preferredStyle: UIAlertControllerStyle.Alert) 
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
        self.presentViewController(alert, animated: true, completion: nil) 


       } 

      } 

     } 

    } 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return self.postCollection.count ?? 0 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("AirCell", forIndexPath: indexPath) 

    // Configure the cell... 

    // cell.textLabel?.text = "test" 

    let weatherSummary = postCollection[indexPath.row] 


     cell.textLabel?.text = String(weatherSummary.userId) 

     cell.detailTextLabel?.text = weatherSummary.title 




    return cell 
} 

} 

mainTableView和空氣電池單元是好的,但是當選擇其他返回相同的信息空氣電池單元?

回答

0

也許我只是想念它,但我可以看到你創建的NSURLSession看起來很好,但是我沒有看到你在創建它之後調用的那個.resume()。如果你不打電話.resume()它甚至永遠不會執行該URLSession。檢查討論here

+0

它的工作。謝謝:) – phantom

+0

爲什麼不賭我的賽格?並只顯示.Air 更新代碼 – phantom