Swift - displaying data for each row in tableview after row is clicked -
having problems displaying data each row in tableview after row clicked, , want 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() // additional setup after loading view, typically nib. self.tableview.delegate = self self.tableview.datasource = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can 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() // additional setup after loading view. var connection = self.array self.label.text = string(stringinterpolationsegment: connection) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }
and got after clicked each row, same data , want when row - item 1 clicked display first item in label in detailviewcontroller , on other rows, , ty
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() // additional setup after loading view, typically nib. self.tableview.delegate = self self.tableview.datasource = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can 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] 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 resources can recreated. }
Comments
Post a Comment