Swift - 在单击行后显示tableview中每行的数据

时间:2015-06-04 12:14:51

标签: swift tableview cell didselectrowatindexpath

在单击行之后显示tableview中每行的数据时出现问题,我想使用didselectRowAtIndexPath,而不是没有它! :)

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
   @IBOutlet weak var tableView: UITableView!

   let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
   let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]

    override func viewDidLoad() {
       super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
       self.tableView.delegate = self
       self.tableView.dataSource = self
    }

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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
        cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("toDetailSegue", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var whatToPass = self.textDataforEachRowWhenClicked
        let detailVC = segue.destinationViewController as! DetailViewController
        detailVC.array = whatToPass
    }
}

DetailViewcontroller.swift

导入UIKit

class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!

var array:[String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    var connection = self.array
    self.label.text = String(stringInterpolationSegment: connection)
}

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

然后我点击了每一行,相同的数据和我想要的时候点击行 - 项目1来显示detailviewcontroller中的标签中的第一项,依此类推其他行,help,ty tableview

2 个答案:

答案 0 :(得分:0)

    import UIKit

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!


    let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
    let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
    var selectedValue:String!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.textToDisplayInCell.count

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
       cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
       return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedValue = textDataforEachRowWhenClicked[indexPath.row] as String
        self.performSegueWithIdentifier("toDetailSegue", sender: self)
    }

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

        let detailVC = segue.destinationViewController as! DetailViewController
        detailVC.message = selectedValue
     }
 }

详细信息视图:

class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!

var message:String!

override func viewDidLoad() {
    super.viewDidLoad()
    var message = self.message
    self.label.text = message
}

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

答案 1 :(得分:0)

在prepareForSegue中,您将整个数组传递给子控制器,而不是单个特定元素。您需要在didSelectRowAtIndexPath中将一些变量(_rowToPass)设置为您要传递的元素,并在prepareForSegue中仅传递该元素。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    _rowToPass = indexPath.row 
    self.performSegueWithIdentifier("toDetailSegue", sender: self)
}



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

    var whatToPass = self.textDataforEachRowWhenClicked[_rowToPass]

    let detailVC = segue.destinationViewController as! DetailViewController

    detailVC.array = whatToPass

 }