How can I run prepareForSegue After didSelectRowAtIndexPath? - ios

I'm trying to get the indexPath.row that user clicked and then check with my array to return a String from that indexPath.row and then send the String via preparforSegue.... But it doesnt work. I think the program runs the preparForSegue BEFORE the didselectRowatIndexPath... How can I do this?
My Code:
import UIKit
import Parse
class CategoriaTableViewController: UITableViewController {
var queryArray: [PFObject] = [PFObject]()
var escolha = 5
override func viewDidLoad() {
super.viewDidLoad()
var query = PFQuery(className:"Categorias")
//querycomlocalidade
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) Restaurantes.")
if let _objects = objects as? [PFObject] {
self.queryArray = _objects
self.tableView.reloadData()
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
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 Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return queryArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CategoriaTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ceculaCategoria", forIndexPath: indexPath) as! CategoriaTableViewCell
let categoria = queryArray[indexPath.row] as! PFObject
//Insere texto
cell.tituloCecula.text = categoria.objectForKey("nome") as! String
//Insere imagens no tableview
if let categoriaImagem = categoria.objectForKey("imagem") as? PFFile {
categoriaImagem.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
cell.imagemCecula.image = UIImage(data:imageData!) }
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
escolha = indexPath.row
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailScene = segue.destinationViewController as! ListaTableViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
let categoriaEscolhida = queryArray[escolha] as PFObject
//Insere texto
detailScene.categoria = categoriaEscolhida.objectForKey("nome") as! String
}
}
}

If you have linked the segue directly from your cell in the storyboard then didSelectRowAtIndexPath will not execute before the segue is triggered.
What you can do is create a segue by Ctrl-dragging from the view controller object in the storyboard to the destination scene and giving the segue an identifier as you would with any other segue. Delete the segue from your cell.
Now you can call performSegueWithIdentifier to trigger the segue and pass the object that was selected as the sender
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// tableView.deselectRowAtIndexPath(indexPath) // Optionally deselect the row for a cleaner appearance
self.performSegueWithIdentifier("mySegueIdentifier", sender: queryArray[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let detailScene = segue.destinationViewController as? ListaTableViewController {
let categoriaEscolhida = sender as! PFObject
detailScene.categoria = categoriaEscolhida.objectForKey("nome") as! String
}
}

You need to create the segue between the two view controllers and trigger it manually:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
escolha = indexPath.row
self.performSegueWithIdentifier("push", sender: self)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
escolha = indexPath.row
self.performSegueWithIdentifier("push", sender: self)
}
Complete with ...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:"push"]) {
// Code
}

Related

View doesn't get updated until the second time loaded

I have a main view which is a table view with a list of countries. When clicking on any country name (cell), another view is loaded via segue which is passing the name of the country to the next view controller's navigation bar title.
The problem is on the first click the title isn't updated, but when I click back button (dismissing the current view) and click on another country name, the second view loads again and shows the previous title that was suppose to be shown on the first attempt.
The code for the first main view controller:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var sectionsArray = [String]()
var sectionsCountries = [Array<AnyObject>]()
#IBOutlet weak var countries: UITableView!
internal func numberOfSections(in tableView: UITableView) -> Int {
// Return the number of sections.
return self.sectionsArray.count
}
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return self.sectionsCountries[section].count
}
internal func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionsArray[section]
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath)
cell.textLabel?.text = self.sectionsCountries[indexPath.section][indexPath.row] as? String
return cell
}
var valueToPass:String!
internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!;
valueToPass = currentCell?.textLabel?.text
performSegue(withIdentifier: "cellSegue", sender: self)
//print(valueToPass)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "cellSegue" {
let destination = segue.destination as! CountryViewController
destination.passedValue = valueToPass
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "http://cyber7.co.il/swift/countries/countries-list.json")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
for result in jsonResult as! [Dictionary<String, AnyObject>]{
self.sectionsArray.append(result["sectionName"] as! String)
self.sectionsCountries.append(result["sectionCountries"] as! Array<String> as [AnyObject])
}
} catch {
print("JSON Processing Failed")
}
DispatchQueue.main.async(execute: { () -> Void in
self.countries.reloadData()
})
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the code for the second view controller:
import UIKit
class CountryViewController: UIViewController {
var passedValue:String!
#IBOutlet weak var navBar: UINavigationBar!
#IBAction func backButton(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
}
override func viewWillAppear(_ animated: Bool) {
self.navBar.topItem?.title = passedValue
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When you have a segue set up from a table view cell to a view controller in the storyboard then it is performed automatically when a cell is selected. Your call to perform a segue in your cell selection method is performing the segue a second time after the first one has already been performed.
Remove your tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method and do all the data passing logic in prepareForSegue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "cellSegue" {
let destination = segue.destination as! CountryViewController
let indexPath = countries.indexPathForSelectedRow
let currentCell = countries.cellForRow(at: indexPath!)
destination.passedValue = currentCell?.textLabel?.text
}
}

How to perform a different segue when in Edit mode (in UITableView)?

I am creating a simple iPhone app using UITableView. I am using the default MasterDetail application template. Right now (in Edit mode) when I press any of the table cells nothing happens. However, when I am in normal mode the detail segue is initiated. How to override the Edit mode so that I initiate a custom segue to go to a different UIViewController.
P.S.: I still want to preserve the inherit delete functionality.
This is my code in my MasterViewController:
class MasterViewController: UITableViewController {
let kFileName: String = "/resolutionData.plist"
var resolutions = [Dictionary<String,String>]()
var achievedResolutions = [Dictionary<String,String>]()
// TO DO create a class to get this array
let iconArray = ["Fish","Fly","Heart","HelpingHand","Melon","Star","Tentacles","Volunteering"]
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
self.navigationItem.rightBarButtonItem = addButton
//extract the path
NSLog(dataFilePath())
/**
* Check where is the sandbox of the application
* and if there is read from the data file and story it to "objects" array
*/
if NSFileManager.defaultManager().fileExistsAtPath(dataFilePath()){
var temp = NSArray(contentsOfFile: dataFilePath()) as! [Dictionary<String,String>]
for res in temp{
if res["isAchieved"] == "Y"{
achievedResolutions.append(res)
}else{
resolutions.append(res)
}
}
//... if there is not - create it
} else {
let data = [["name":"Resolution name test","startingDate":"24-11-15","achievingDate":"01-01-2016","icon":iconArray[0],"isAchieved":"N"] as NSDictionary] as NSArray
//if the file does not exist...
if !NSFileManager.defaultManager().fileExistsAtPath(dataFilePath()){
//... create it
NSFileManager.defaultManager().createFileAtPath(dataFilePath(), contents: nil, attributes: nil)
}
//write to it
data.writeToFile(dataFilePath(), atomically: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func insertNewObject(sender: AnyObject) {
performSegueWithIdentifier("editDetails", sender: sender)
}
func saveDateToFile(){
let data = resolutions as NSArray
data.writeToFile(dataFilePath(), atomically: true)
}
func notifyTableViewForNewInsertion(){
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object = resolutions[indexPath.row] as Dictionary
(segue.destinationViewController as! DetailViewController).detailItem = object
}
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Active"
}else{
return "Achieved"
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return resolutions.count
}else{
// TODO replace that with an actual array count
return achievedResolutions.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if indexPath.section == 0{
let object: AnyObject? = resolutions[indexPath.row] ["name"]
cell.textLabel!.text = object as? String
cell.detailTextLabel!.text = resolutions[indexPath.row]["achievingDate"]
cell.imageView!.image = UIImage(named: resolutions[indexPath.row]["icon"]!)
} else {
let object: AnyObject? = achievedResolutions[indexPath.row] ["name"]
cell.textLabel!.text = object as? String
cell.detailTextLabel!.text = resolutions[indexPath.row]["achievingDate"]
cell.imageView!.image = UIImage(named: resolutions[indexPath.row]["icon"]!)
}
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
resolutions.removeAtIndex(indexPath.row)
saveDateToFile()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func dataFilePath() -> String{
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
//get the first path and convert it to str
let docDicrectyory: String = paths[0] as! String
return "\(docDicrectyory)\(kFileName)"
}
}
Instead of performing your segue directly from the storyboard, add a UITableViewDelegate method:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView.editing {
performSegueWithIdentifier("id_Segue_Editing_VC", sender: tableView.cellForRowAtIndexPath(indexPath))
} else {
performSegueWithIdentifier("id_Segue_Standard_VC", sender: tableView.cellForRowAtIndexPath(indexPath))
}
}
Set the sender to the selected cell -- this matches the default UIKit behavior.
Then in your prepareForSegue method you can add custom value to your view controllers according to the segue identifier.
Override willBeginEditingRowAtIndexPath: function. this willbe call before start editing. there you can initialize a global variable.
#property(nonatomic, String) BOOL editing
and in the
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (editing) {
if([[segue identifier] isEqualToString:#"identifierOne"]){
}else if([[segue identifier] isEqualToString:#"identifierTwo"]){
}
}
}
There is a table view property for that:
self.tableView.allowsSelectionDuringEditing = YES;

iOS unwind CollectionView at didSelectItemAtIndexPath

So i have created an app where users create custom workouts.
You enter the first view add a x number of rounds,than you click on a button in the tableview where you add rounds.That click opens another activity with a collectionview.
So the problem is here when i click on an element witch represents the exercise that needs to be in the round it sends me an empty result.By default if i add a value to that string it works.
I have noticed that the unwind happens before my didSelectItemAtIndexPath
import UIKit
class InsertWorkout: UIViewController{
#IBOutlet var InsertRoundTable: UITableView!
#IBOutlet weak var txtName: UITextField!
var RoundNumber : NSMutableArray = ["Round 1"]
var RoundLabel : NSMutableArray = [""]
var RoundExercise : NSMutableArray = ["add-1"]
var RoundExerciseImages : NSMutableArray = ["providno"]
var RoundNumber_Count=1
var RoundNumber_Add_Counter=1
var studentData : StudentInfo!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UIButton Action methods
#IBAction func btnBackClicked(sender: AnyObject)
{
self.navigationController?.popViewControllerAnimated(true)
}
#IBAction func btnSaveClicked(sender: AnyObject)
{
if(txtName.text == "")
{
Util.invokeAlertMethod("", strBody: "Please enter workout name.", delegate: nil)
}
else
{
let studentInfo: StudentInfo = StudentInfo()
studentInfo.workout_name = txtName.text!
studentInfo.workout_benefit_1=" CUSTOM "
studentInfo.workout_benefit_2=" WORKOUT "
studentInfo.workout_benefit_3=""
studentInfo.workout_requiremnets="arsutech.com"
studentInfo.workout_time="4min"
let isInserted = ModelManager.getInstance().addWorkoutData(studentInfo)
if isInserted {
Util.invokeAlertMethod("", strBody: "Workout added", delegate: nil)
} else {
Util.invokeAlertMethod("", strBody: "Error while adding workout.", delegate: nil)
}
self.navigationController?.popViewControllerAnimated(true)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
InsertRoundTable.endEditing(true)
}
//UITableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return RoundNumber.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : InsertRoundCell! = tableView.dequeueReusableCellWithIdentifier("InsertRoundCell") as! InsertRoundCell
if(cell == nil)
{
cell = NSBundle.mainBundle().loadNibNamed("InsertRoundCell", owner: self, options: nil)[0] as! InsertRoundCell;
}
let exerviseName = RoundNumber[indexPath.row]
let exerciseLabels = RoundLabel[indexPath.row]
let exerciseImage = RoundExercise[indexPath.row]
let exerciseImage_Holder = RoundExerciseImages[indexPath.row]
cell.insert_label_Round.text = exerviseName as? String
cell.addWorkout_Label.text = exerciseLabels as? String
cell.addExercise_Holder?.image = UIImage(named: exerciseImage_Holder as! String) as UIImage?
cell.addWorkoutBut.setBackgroundImage(UIImage(named: exerciseImage as! String) as UIImage?, forState: UIControlState.Normal)
cell.addWorkoutBut.tag = indexPath.row
cell.addWorkoutBut.addTarget(self, action: "logAction:", forControlEvents: .TouchUpInside)
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell as InsertRoundCell
}
#IBAction func logAction(sender: UIButton){
//self.RoundNumber.replaceObjectAtIndex(sender.tag, withObject: "Ezel")
//let titleString = self.RoundNumber[sender.tag] as? String
//Util.invokeAlertMethod("", strBody: titleString!, delegate: nil)
self.InsertRoundTable.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let destinationVC = segue.destinationViewController as! ChoseExercise
destinationVC.id=sender.tag
RoundNumber_Count=sender.tag
}
#IBAction func unwinndChoseExercise(segue:UIStoryboardSegue){
if let svc = segue.sourceViewController as? ChoseExercise{
self.RoundLabel.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_label)")
self.RoundExercise.replaceObjectAtIndex(RoundNumber_Count, withObject: "providno")
self.RoundExerciseImages.replaceObjectAtIndex(RoundNumber_Count, withObject: "\(svc.exercise_image)")
self.InsertRoundTable.reloadData()
}
}
//Add Round
#IBAction func addRound(sender: AnyObject) {
if(RoundNumber_Add_Counter<12){
RoundNumber_Add_Counter++
self.RoundNumber.addObject("Round \(RoundNumber_Add_Counter)")
self.RoundLabel.addObject("")
self.RoundExercise.addObject("add-1")
self.RoundExerciseImages.addObject("providno")
self.InsertRoundTable.reloadData()
}else{
Util.invokeAlertMethod("", strBody: "This is the maximum number of rounds", delegate: nil)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.exercisesNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellexercises_insert", forIndexPath: indexPath) as! Insert_ExerciseCollectionViewCell
cell.insert_exerciseImage?.image = self.exercisesImages[indexPath.row]
cell.insert_exerciseLabel?.text = self.exercisesNames[indexPath.row]
cell.insert_exercisesHardnessImg?.image = self.exercisesHardnessImg[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
exercise_label="\(exercisesNames[indexPath.row])"
exercise_image="\(exercises_Exervise_Row_Names[indexPath.row])"
dismissViewControllerAnimated(true, completion: nil)
}
Here is how unwinding works:
First, put your IBAction unwind(segue:UIStoryboardSegue) in the vc where you intend to go back (presenter vc, InsertWorkout in this case).
Second, go to storyboard and choose the vc where you'll unwind from (collection view in this case) and ctrl-drag from the button/cell that causes the unwind (collection view cell in your case) to the exit button on top of that same vc and you should see a pop-up with your IBAction unwind method name under 'Selection Segue'.
Third, implement prepareforsegue in the current vc (collection vc in this case) to pass your data back.
class InsertWorkout
{
IBAction func unwind(segue:UIStoryboardSegue) {
//use the data passed from prepare to update your ui or you can also communicate to the sender using segue.sourceVC
}
}
class CollectionVC
{
override func prepareForSegue(segue: UIStoryboadSegue, sender: AnyObject?){
if segue.identifier == "whatever you put in storyboard for segue id" {
if let inserWorkoutVC = segue.destinationViewController as? InsertWorkout {
//pass the data here but don't attempt to update your destination's view ui!!,
}
}
}
}

Confused with UITableViewDataSource Protocol. Conflicts with previous

I have an error that idk how to fix it. The error is: "Definition conflicts with previous value." I have copied the code below and starred the portion which is flagged.
Thanks in advance.
import UIKit
import Parse
class ScienceTableViewController: UITableViewController, UITableViewDataSource {
var dataRecords = [Data]()
override func viewDidLoad() {
super.viewDidLoad()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
dataRecords = [Data]()
let query = PFQuery(className: "data")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
let data = Data(objectID: object.valueForKey("objectId")! as! String, title: object.valueForKey("title")! as! String, date: object.valueForKey("date")! as! String, information: object.valueForKey("information")! as! String)
self.dataRecords.append(data)
self.tableView.reloadData()
}
} else {
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return dataRecords.count > 0 ? 1 : 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataRecords.count
}
**override func tableView(tableView: UITableView**, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// Configure the cell...
let title = dataRecords[indexPath.row].title
let date = dataRecords[indexPath.row].date
let information = dataRecords[indexPath.row].information
cell.textLabel?.text = title
cell.detailTextLabel?.text = date
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let controller: ViewController = segue.destinationViewController as! ViewController
if segue.identifier == "newData" {
controller.objectID = ""
} else if segue.identifier == "showData" {
let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
controller.objectID = dataRecords[indexPath.row].objectID
}
}
}}
I am also getting problems with xCode where it claims "Xcode encountered a problem. Source editor functionality is limited. Attempting to restore" and the editor does not recognize the code for brief moments.
I believe your issue is with the class declaration line that has UITableViewDataSource as a protocol. A UiTableViewController already is a data source so that is redundant and should be removed.

Getting error in handling the selected row in table in iOS swift

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("SearchTableViewCellIdentifier") as! SearchTableViewCell
var item = self.searchResult[indexPath.row] as? PFObject
cell.post = item
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndex rowIndex: Int)
{
let indexPath = tableView.indexPathForSelectedRow()
let currentCell = tableView.cellForRowAtIndexPath(indexPath!)! as UITableViewCell
println(currentCell.textLabel!.text)
}
I am not getting the actual value. I am getting nil on printing currentCell.textLabel!.text
just remove override
class yourclassName: UIViewController, UITableViewDataSource, UITableViewDelegate
var cod: AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, didSelectRowAtIndex rowIndex: Int)
{
//Handle row selection
// choice 1
let indexPath = tableView.indexPathForSelectedRow();
// if it is not work follow second option
cod = self.searchResult[indexPath.row] as? PFObject
// choice 2
cod = self.searchResult[rowIndex] as? PFObject
println(cod)
self.performSegueWithIdentifier("yourSegueName", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "yourSegueName") {
var svc = segue!.destinationViewController as secondViewController;
svc.toPass = cod
}
}
in your second VC create this string
var toPass:String!

Resources