2016-04-05 7 views
1

2つのテーブルビューがあります.1つ目は6つの都市を表示し、2つ目のビュー(詳細テーブルビュー)は最初のテーブルビューで選択したセルに基づいて詳細を表示します。ユーザーが最初のテーブルビューで都市を選択するたびに、対応する配列を使用して2番目のビューコントローラを作成したいと思います。私の質問は、prepareforsegueでどのように達成するかです。Tableviewのデータをテーブルビューの詳細に渡す

//First tableview as initial view 



import UIKit 
import Alamofire 
import SwiftyJSON 

class LandingPageTableViewController: UITableViewController { 

@IBOutlet var tableview: UITableView! 


//predfined array to to holde the data below 

var city = [String]() 
var subtitle = [String]() 
var image = [String]() 

//arrays to hold avaible buildings in the city 

var brickellBuildings  = [[String:AnyObject]]()// 
var miamiBuilBuildings = [[String:AnyObject]]() 
var miamiBeachBuildings = [[String:AnyObject]]()// 
var coconotGroveBuildins = [[String:AnyObject]]()// 
var corolGbalesBuilduings = [[String:AnyObject]]() 
var dowtownMiamiBuildings = [[String:AnyObject]]()// 
var southBeachBuildings = [[String:AnyObject]]() 



override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 

    tableview.showsHorizontalScrollIndicator = false 
    tableview.showsVerticalScrollIndicator = false 
    tableview.dataSource = self 
    tableview.delegate = self 


city = ["Miami","Brickell","Miami Beach","Down town Miami","South Beach","Corol Gables","Coconut Grove"] 


    subtitle = ["The fab and glam life "," Condos on the highrise","Don't call a comeback","Come on feel the noise ","Push it to the limit","Push it to the limit","Push it to the limit"] 

    image = ["Miami","brickell","miami_beach","downtown_miami","south_beach","coral_gables","coconut_grove"] 



} 

override func viewDidAppear(animated: Bool) { 
    super.viewWillAppear(true) 

    runRequestForbrickell() 
    runRequestForMiamiBeach() 
    runRequestForcocotGrove() 
    runRequestForMiami() 
} 





    func runRequestForbrickell() { 

    Alamofire.request(.GET, "http://android.goidx.com/condos/?tag=brickell&user_id=4").validate().responseJSON { response in 

     if let value = response.result.value { 
      let json = JSON(value) 

      if let resData = json.arrayObject { 

       self.brickellBuildings = resData as! [[String:AnyObject]] 

       // print(resData) 
      } 

      if self.brickellBuildings.count > 0 { 

       self.tableview.reloadData() 
      } 
      } 
      } 
      } 

    func runRequestForMiamiBeach() { 


    Alamofire.request(.GET, "http://android.goidx.com/condos/?tag=south beach&user_id=4").validate().responseJSON { response in 

     if let value = response.result.value { 
      let json = JSON(value) 

      if let resData = json.arrayObject { 

       self.miamiBeachBuildings = resData as! [[String:AnyObject]] 

       // print(resData) 
      } 

      if self.miamiBeachBuildings.count > 0 { 

       self.tableview.reloadData() 
      } 

      } 
      } 
      } 


    func runRequestForcocotGrove() { 

Alamofire.request(.GET, "http://android.goidx.com/condos/?tag=coconut%20grove&user_id=4").validate().responseJSON { response in 

    if let value = response.result.value { 
     let json = JSON(value) 

     if let resData = json.arrayObject { 

      self.coconotGroveBuildins = resData as! [[String:AnyObject]] 

      // print(resData) 
     } 

     if self.coconotGroveBuildins.count > 0 { 

      self.tableview.reloadData() 
     } 
     } 
     } 
     } 



     func runRequestForMiami() { 

Alamofire.request(.GET, "http://android.goidx.com/condos/?tag=downtown miami&user_id=4").validate().responseJSON { response in 

    if let value = response.result.value { 
     let json = JSON(value) 

     if let resData = json.arrayObject { 

      self.miamiBuilBuildings = resData as! [[String:AnyObject]] 

      // print(resData) 
     } 

     if self.miamiBuilBuildings.count > 0 { 

      self.tableview.reloadData() 
     } 
     } 
     } 
     } 



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 

     switch identifier { 


     case "showbuildings": 
      let buildingdDetailVC = segue.destinationViewController as! CtiesTableViewController 

      if let indexpath = self.tableview.{ 


      } 

     default: break 



     } 
     } 
     } 


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

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
    } 

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Celllanding", forIndexPath: indexPath) as! LandingTableViewCell 


    let holder_city = city[indexPath.row] 
    let holder_subtitle = subtitle[indexPath.row] 
    cell.citynamelabel.text? = holder_city 
    cell.descriptionHolder.text? = holder_subtitle 


    cell.imageCity.image = UIImage(named: image[indexPath.row]) 

    return cell 
    } 



/* 
// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return false if you do not want the specified item to be editable. 
    return true 
} 
*/ 

/* 
// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     // Delete the row from the data source 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    }  
    } 
*/ 

/* 
// Override to support rearranging the table view. 
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

     } 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return false if you do not want the item to be re-orderable. 
    return true 
    } 
*/ 

/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
    } 
*/ 


override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 300.0 
    } 



    } 









    //Second tableview as a detail tableview 





    import UIKit 
    import Alamofire 
    import SwiftyJSON 

    class CtiesTableViewController: UITableViewController { 

@IBOutlet var tableview: UITableView! 


var images = [String]() 



    // Strings to store the data passed from the first tableview 
    var datareceiver1: String = "" 
    var datareceiver2: String = "" 
    var datareceiver3: String = "" 
    var datareceiver4: String = "" 
    var datareceiver5: String = "" 

    // array of dictionary to append the data received from first tableview 
    var Data1 = [[String:AnyObject]]() //Array of dictionary 
    var Data2 = [[String:AnyObject]]() //Array of dictionary 
    var Data3 = [[String:AnyObject]]() //Array of dictionary 
    var Data4 = [[String:AnyObject]]() //Array of dictionary 
    var Data5 = [[String:AnyObject]]() //Array of dictionary 
    var Data6 = [[String:AnyObject]]() //Array of dictionary 


func refresh(sender:AnyObject) 
{ 
    // Updating your data here... 

    self.tableView.reloadData() 
    self.refreshControl?.endRefreshing() 
} 


    override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 

    self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged) 
    refreshControl!.backgroundColor = UIColor.grayColor() 
    refreshControl!.tintColor = UIColor.whiteColor() 

    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 



     } 


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

     } 



    override func viewWillAppear(animated: Bool) { 


    super.viewWillAppear(animated) 

    tableview.showsHorizontalScrollIndicator = false 
    tableview.showsVerticalScrollIndicator = false 
    tableview.dataSource = self 
    tableview.delegate = self 


    self.tableview.rowHeight = 300.0 



    } 


// MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

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

    return Data.count 

} 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PlacesTableViewCell 

    // Configure the cell... 




    return cell 
    } 


    } 

答えて

1

これはprepareForSegueメソッドを呼び出す最初TableViewController

var arrayToBePassed: YourType? 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    switch indexPath.row { 
    case 0: 
     arrayToBePassed = yourArray 

    default: 
     break 
    } 
    self.performSegueWithIdentifier("showbuildings", sender: self) 
} 

にアレイを通過する保持する変数を宣言し、overrideUITableViewDelegatedidSelectRowAtIndexPath方法。今、第二UITableViewControllerに、あなたが言及した配列型のvarを宣言し、prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    switch identifier { 
     let buildingdDetailVC = segue.destinationViewController as! CtiesTableViewController 
     buildingdDetailVC.arrayInSecondTVC = arrayToBePassed 

    default: 
     break 
    } 
} 

PSを変更します。それは作られた一般的な間違いがあるとして、あなたがいないoverridingdidDeselectRowAtIndexPathをしていることを確認してください。あなたの配列を言うとき

+0

。 secondviewの配列について話していますか? –

+0

'yourArray'は最初に' UITableViewController'で選択した行の詳細を保持する配列です。 'didSelectRowAtIndexPath'では' arrayToBePassed'という同じ型の別の共通配列にコピーされます。 'arrayInSecondTVC'は、第2の' TableViewController'クラスで宣言される配列で、最初のコントローラの 'prepareForSegue'では、選択された行の詳細をこの配列に設定します。 – Mathews

+0

それは働いた!それは非常に素晴らしいアイデアメイトだった –

関連する問題