0

This is the code that I have so far but I want to make it clickable. Once you click on an item it can take you to another page.

import UIKit
import CoreData

class ViewController: UIViewController,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var people = [NSManagedObject]()

@IBAction func addName(sender: AnyObject) {
    let alert = UIAlertController(title: "New Client",
        message: "Add a new client",
        preferredStyle: .Alert)

    let saveAction = UIAlertAction(title: "Save",
        style: .Default,
        handler: { (action:UIAlertAction) -> Void in

            let textField = alert.textFields!.first
            self.saveName(textField!.text!)


            self.tableView.reloadData()
    })

    let cancelAction = UIAlertAction(title: "Cancel",
        style: .Default) { (action: UIAlertAction) -> Void in
    }

    alert.addTextFieldWithConfigurationHandler {
        (textField: UITextField) -> Void in
    }

    alert.addAction(saveAction)
    alert.addAction(cancelAction)

    presentViewController(alert,
        animated: true,
        completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()
    title = "Clients"
    tableView.registerClass(UITableViewCell.self,
        forCellReuseIdentifier: "Cell")

}

func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
        return people.count
}

func tableView(tableView: UITableView,
    cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {

        let cell =
        tableView.dequeueReusableCellWithIdentifier("Cell")

        let person = people[indexPath.row]

        cell!.textLabel!.text =
            person.valueForKey("name") as? String


        return cell!
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func saveName(name: String) {
    //1
    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let entity =  NSEntityDescription.entityForName("Person",
        inManagedObjectContext:managedContext)

    let person = NSManagedObject(entity: entity!,
        insertIntoManagedObjectContext: managedContext)

    //3
    person.setValue(name, forKey: "name")


    //4
    do {
        try managedContext.save()
        //5
        people.append(person)
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }
}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    //1
    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let fetchRequest = NSFetchRequest(entityName: "Person")


    //3
    do {
        let results =
        try managedContext.executeFetchRequest(fetchRequest)
        people = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }
}


}
Julian E.
  • 4,687
  • 6
  • 32
  • 49
kaherna8
  • 161
  • 1
  • 9
  • Make table view selection style as "No Selection" and use tap gesture for making table view clickable. – Pushpa Y Dec 05 '15 at 06:51

1 Answers1

0

You can make the view controller be the tableView delegate and implement tableView:didSelectRowAtIndexPath: to present the new page view controller or perform a segue. Your class definition becomes:

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
  // your existing code
    func tableView(_ tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // either present the view controller for new page or perform the Segue
    }
}
farhadf
  • 1,918
  • 3
  • 19
  • 27