I have two view controllers as UITableViewController
and DestinationViewController
. The content of DestinationViewController
should change according to tapped UITableViewCell. I tried this tutorial but this one didn't make sense. How can I determine which cell is tapped and how can I perform segue when the cell tapped?
EDIT:
UITableViewController:
let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var myIndex: Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
table1View.deselectRowAtIndexPath(indexPath, animated: true)
myIndex = indexPath.row
self.performSegueWithIdentifier("showClassesForSchedule", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "showClassesForSchedule":
let destinationViewController = segue.destinationViewController as? ScheduleAsClassTableViewController// set here нour destionation VC
if destinationViewController != nil {
destinationViewController?.tableArray.append(days[myIndex!])
destinationViewController?.tableArray = days
print(days[myIndex!])
}
default: break
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return days.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("scheduleDaysTextCellIdentifier", forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = days[row]
return cell
}
DestinationViewController:
var tableArray = [String]()
@IBOutlet var table1View: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
print(tableArray)
}
@IBOutlet weak var comingLabelAsNormalView: UILabel!
@IBOutlet weak var comingName: UILabel!
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 tableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = table1View.dequeueReusableCellWithIdentifier("scheduleDayClassIdentifier", forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = tableArray[row]
return cell
}