如何使UITableView中的单元格可编辑?

时间:2020-11-29 19:35:35

I have 2 view controllers. One is a UITableView and the other is the VC that lets me add a cell in the table.

我有两个视图控制器。一个是UITableView另一个是VC允许我在表格中添加一个单元格。

PeopleListViewController:

PeopleListViewController:

var people = [Person]()
override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return people.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("personCell", forIndexPath: indexPath) as UITableViewCell

        // Configure the cell...
        cell.textLabel.text = (people[indexPath.row]).dateTime //Each row will say the date of the person.
        return cell
    }

    @IBAction func cancel(segue:UIStoryboardSegue){

    }

    @IBAction func done(segue:UIStoryboardSegue){
        var addPersonVC = segue.sourceViewController as AddPersonViewController

        var newPerson = Person()
        newPerson.dateTime = addPersonVC.date

        people.append(newPerson)
    }

AddPersonViewController:

AddPersonViewController:

@IBOutlet weak var dateAndTime: UITextField!
    let date = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
    override func viewDidLoad() {
        super.viewDidLoad()
        dateAndTime.text = "\(date)"
    }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "doneSegue" {

        }
    }

Right now what it does is comes up with a tableview. When I click the add button it takes me to the addpersonvc and automatically preloads the date. When I hit done, it goes back to the peoplelistvc and fills the date in the table cell.

现在它的作用是生成一个tableview。当我点击添加按钮时,它会把我带到addpersonvc中并自动预加载日期。当我点击完成时,它返回到peoplelistvc并填充表格单元格中的日期。

How can I make it so that when I click on a cell it will let me go back to the addpersonvc and edit it? Thank you.

我怎么做才能让我点击单元格时回到addpersonvc并编辑它呢?谢谢你!

2 个解决方案

#1


1  

One way is you can create your own custom cell. Create a custom cell class, with a .xib file. Register the NIB in your viewController. Then you can use your custom cell, editable in the .xib file.

一种方法是创建您自己的自定义单元格。创建一个带有.xib文件的自定义单元格类。在视图控制器中注册NIB。然后可以使用.xib文件中的自定义单元格。

If you register the custom cell in your view controller, in your cellForRowAtIndexPath tableview function, you can declare your cell as

如果在视图控制器中注册自定义单元格,在cellForRowAtIndexPath表视图函数中,可以将单元格声明为

var nib : UINib = UINib(nibName: "CustomCell", bundle: nil)
table.registerNib(nib, forCellReuseIdentifier: "myCell")

var cell: customCell = tableView.dequeReusableCellWithIdentifier("myCell" , forIndexPath: indexPath) as customCell

I read your question better. This is how you edit the visual appearance of your cell. My bad.

我把你的问题看得更清楚了。这就是如何编辑单元格的视觉外观。我的坏。

You need to implement the didSelectRowAtIndexPath method.

您需要实现didSelectRowAtIndexPath方法。

#2


1  

Nates on the right track. There are two ways to do this.

在正确的轨道上。有两种方法可以做到这一点。

  1. Programatically:
  2. 编程:

To do this programatically implement the didSelectRowAtIndexPath method and then instantiate a new controller. You can then use the indexPath variable to get the index from the data array that your table is consuming.

要实现这个编程实现didSelectRowAtIndexPath方法,然后实例化一个新的控制器。然后可以使用indexPath变量从表使用的数据数组中获取索引。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var vc : UIViewController! = self.storyboard!.instantiateViewControllerWithIdentifier("practice") as UIViewController
}
  1. Using Storyboards
  2. 使用故事板

If you want to do this using storyboards, create a viewController and a tableViewController. Than right drag from one of your table view cells, to the viewController object. A popup menu will appear, and choose "Show", this will create a segue that will let you go to that view when you select one of the cells. You'll then have to write the code to populate it in the UIViewController

如果你想用故事板来做这个,创建一个viewController和tableViewController。而不是从一个表格视图单元格拖到viewController对象。弹出菜单将出现,并选择“Show”,这将创建一个segue,当您选择一个单元格时,该segue将允许您访问该视图。然后你必须写代码来填充UIViewController

#1


1  

One way is you can create your own custom cell. Create a custom cell class, with a .xib file. Register the NIB in your viewController. Then you can use your custom cell, editable in the .xib file.

一种方法是创建您自己的自定义单元格。创建一个带有.xib文件的自定义单元格类。在视图控制器中注册NIB。然后可以使用.xib文件中的自定义单元格。

If you register the custom cell in your view controller, in your cellForRowAtIndexPath tableview function, you can declare your cell as

如果在视图控制器中注册自定义单元格,在cellForRowAtIndexPath表视图函数中,可以将单元格声明为

var nib : UINib = UINib(nibName: "CustomCell", bundle: nil)
table.registerNib(nib, forCellReuseIdentifier: "myCell")

var cell: customCell = tableView.dequeReusableCellWithIdentifier("myCell" , forIndexPath: indexPath) as customCell

I read your question better. This is how you edit the visual appearance of your cell. My bad.

我把你的问题看得更清楚了。这就是如何编辑单元格的视觉外观。我的坏。

You need to implement the didSelectRowAtIndexPath method.

您需要实现didSelectRowAtIndexPath方法。

#2


1  

Nates on the right track. There are two ways to do this.

在正确的轨道上。有两种方法可以做到这一点。

  1. Programatically:
  2. 编程:

To do this programatically implement the didSelectRowAtIndexPath method and then instantiate a new controller. You can then use the indexPath variable to get the index from the data array that your table is consuming.

要实现这个编程实现didSelectRowAtIndexPath方法,然后实例化一个新的控制器。然后可以使用indexPath变量从表使用的数据数组中获取索引。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var vc : UIViewController! = self.storyboard!.instantiateViewControllerWithIdentifier("practice") as UIViewController
}
  1. Using Storyboards
  2. 使用故事板

If you want to do this using storyboards, create a viewController and a tableViewController. Than right drag from one of your table view cells, to the viewController object. A popup menu will appear, and choose "Show", this will create a segue that will let you go to that view when you select one of the cells. You'll then have to write the code to populate it in the UIViewController

如果你想用故事板来做这个,创建一个viewController和tableViewController。而不是从一个表格视图单元格拖到viewController对象。弹出菜单将出现,并选择“Show”,这将创建一个segue,当您选择一个单元格时,该segue将允许您访问该视图。然后你必须写代码来填充UIViewController