Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使UITableView中的单元格可编辑?_Ios_Uitableview_Swift - Fatal编程技术网

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

Ios 如何使UITableView中的单元格可编辑?,ios,uitableview,swift,Ios,Uitableview,Swift,我有两个视图控制器。一个是UITableView,另一个是VC,它允许我在表中添加单元格 PeopleListViewController: var people = [Person]() override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //

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

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

        }
    }
AddPersonViewController:

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

        }
    }
现在它所做的是提供一个tableview。当我点击add按钮时,它会将我带到addpersonvc并自动预加载日期。当我点击“完成”时,它会返回peoplelistvc并在表格单元格中填充日期

我怎样才能使它,当我点击一个单元格时,它会让我回到addpersonvc并编辑它?
谢谢。

一种方法是您可以创建自己的自定义单元格。使用.xib文件创建自定义单元格类。在viewController中注册NIB。然后可以使用自定义单元格,可在.xib文件中编辑

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

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

var cell: customCell = tableView.dequeReusableCellWithIdentifier("myCell" , forIndexPath: indexPath) as customCell
我更了解你的问题。这是编辑单元格视觉外观的方式。我的错


您需要实现didselectrowatinexpath方法

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

  • 在程序上:
  • 为此,通过编程实现DidSelectRowatineXpath方法,然后实例化一个新的控制器。然后可以使用indexPath变量从表使用的数据数组中获取索引

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
            var vc : UIViewController! = self.storyboard!.instantiateViewControllerWithIdentifier("practice") as UIViewController
    }
    
  • 使用故事板

  • 如果要使用故事板执行此操作,请创建viewController和tableViewController。然后右键从一个表视图单元格拖动到viewController对象。将出现一个弹出菜单,并选择“显示”,这将创建一个序列,当您选择其中一个单元格时,该序列将允许您转到该视图。然后,您必须编写代码以将其填充到UIViewController中

    “如何使UITableView中的单元格可供使用?”我想您的意思是“可编辑”。UITableViewCells没有那么美味。因此,当您点击单元格(单元格上有日期)时,您想显示
    AddPersonViewController
    ,并向视图控制器显示预填充的日期吗?AddPersonViewController将有更多字段,如名字、姓氏、,等等。当你点击一个单元格时,它会从那个人那里提取信息