Search code examples
swifttableviewcelldidselectrowatindexpath

Swift - displaying data for each row in tableview after row is clicked


Having problems with displaying data for each row in tableview after row is clicked, and i want to use didselectRowAtIndexPath, not without it! :)

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

import 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.
}

and i got this after i clicked each row, the same data and i want when row - item 1 is clicked to display first item in label in detailviewcontroller and so on with other rows, help , ty tableview


Solution

  •     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
         }
     }
    

    In Detail View:

    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.
    }