I'm still new to Swift, so please bear with me.
Currently I have loaded data from a JSON file and that is then displayed onto the individual cells.
I have a custom cell class which has the necessary label outlets (name and number).
What I want to do, is to retrieve data from a specified labels text when on a given row then pass it onto another ViewController.
e.g. Row 3 has two labels, "Data" and "2004". I have selected row 3 and would like to get this cells "2004" value and assign it to a variable that can be then passed to a different ViewController. ("2004" would be considered the number label outlet in my custom cell class i.e. cell.number)
Here is some code that may be of help:
Table View Controller
import UIKit
class TableViewController: UITableViewController, UINavigationControllerDelegate {
@IBOutlet var segmentedSortOption: UISegmentedControl!
var array : NSArray = DataClass.dataStruct.jsonResult["data"] as NSArray
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as cell
var upperCasedNames = array[indexPath.row]["name"] as? String
if segmentedSortOption.selectedSegmentIndex == 0 {
myCell.No.text = array[indexPath.row]["no"] as? String
} else if segmentedSortOption.selectedSegmentIndex == 1 {
if let unsortedEvents = DataClass.dataStruct.jsonResult["data"] as NSArray {
let descriptor = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:")
let aToZ = unsortedEvents.sortedArrayUsingDescriptors([descriptor])
myCell.No.text = aToZ[indexPath.row]["no"] as? String
}
}
return myCell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "selectedItem" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let destinationController = segue.destinationViewController as ViewController
destinationController.eventData = indexPath.row as Int
}
}
Custom Cell Class
import UIKit
class cell: UITableViewCell {
@IBOutlet var name: UILabel!
@IBOutlet var number: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
If you have any more questions, please do ask.
If you have a better alternative, please do suggest that as well.
Thanks!
EDIT: Forgot to mention a key issue. There is a segmented control that reorganises the data from the given order in the JSON file to an alphabetical order. The cells indexPath.row becomes useless in the instance of the A-Z view as the order is completely different.