如何让tableview成为可能,以便它可以带我到另一个页面?

时间:2015-12-05 05:26:54

标签: swift2 xcode7.1

这是我到目前为止的代码,但我想让它可以点击。点击一个项目后,它可以带你到另一个页面。

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)")
    }
}


}

1 个答案:

答案 0 :(得分:0)

您可以使视图控制器成为tableView委托并实现tableView:didSelectRowAtIndexPath:以呈现新的页面视图控制器或执行segue。您的班级定义变为:

<div class="container">
  <div class="imgContainer">
    <img src="http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png" />
  </div>
  <div class="textContainer">
    <div class="row1">
      <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
    </div>
    <div class="row2">
      <span>More text.</span>
    </div>
  </div>
</div>