如何使表格视图可点击,以便将我带到另一个页面?

Posted

技术标签:

【中文标题】如何使表格视图可点击,以便将我带到另一个页面?【英文标题】:How can I make the tableview cliackable so it can take me to another page? 【发布时间】:2015-12-05 05:26:54 【问题描述】:

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

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】:

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

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
    

【讨论】:

以上是关于如何使表格视图可点击,以便将我带到另一个页面?的主要内容,如果未能解决你的问题,请参考以下文章

触摸表格视图中的单元格后标签栏消失

如何制作 parse.com 注销按钮以将我带到另一个让我登录或注册的活动?

在 UIViewController 中使单元格可点击

如何使用 Javascript 打开另一个 HTML 页面

如何将我的表格视图倒置,以便新添加的单元格出现在顶部而不是按钮?

滑动手势识别器对我不起作用