2014-06-12 2 views
1
import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var tableView: UITableView 
    var dict1:Dictionary<Int,String> = [ 0:"One",1:"TwO",2:"Three"]; 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return self.dict1.count; 
    } 

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

     cell.textLabel.text = self.dict1[indexPath.row] 

     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
     println("You selected cell #\(indexPath.row)!") 

    }    
} 
+0

selectedIndex 값을 다른 ViewController로 전달해야합니까? – PREMKUMAR

답변

5

당신은 명시 적으로 행이 선택되어 있는지 감지 할 때의 데이터를 새로운 뷰 컨트롤러를 구축하고 전달할 수 있습니다

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath path: NSIndexPath!) { 
    let entry = news[path.row] as NSDictionary 
    let url = entry["link"] as NSString 
    let secondViewController = 
     self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") 
     as SecondViewController 

    // pass the relevant data to the new sub-ViewController 
    secondViewController.url=url; 
    // tell the new controller to present itself 
    self.navigationController.pushViewController(secondViewController, animated: true) 
} 

및 SecondViewController.swift에서

이, 관련 데이터를 보유 할 변수를 추가합니다.

class SecondViewController: UIViewController { 
    var url: String? = "" 
} 
+0

이것은 빠른 2에서 작동하지 않습니다. – DeyaEldeen

관련 문제