如何制作可编辑的表格单元格

时间:2022-06-04 06:29:37

I'm looking for a way to make UITableView editable like in phone app in iOS when I want to edit the contact data, user should view data and shall be able to press on edit in order to edit the data.

我正在寻找一种方法让UITableView像iOS中的手机应用程序一样可编辑,当我想编辑联系人数据时,用户应该查看数据并且能够按下编辑以编辑数据。

I've been searching for a way to do this for weeks on the web, but I didn't find anything

我已经在网上搜索了几周的方法,但我没有找到任何东西

2 个解决方案

#1


1  

There are two methods that I use for editing table cells. The first method is tableView(_: canEditRowAtIndexPath indexPath:). In this method you specify the section and row of the table view that can be edited. For instance>

我使用两种方法来编辑表格单元格。第一种方法是tableView(_:canEditRowAtIndexPath indexPath :)。在此方法中,您可以指定可以编辑的表视图的节和行。例如>

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

        if indexPath.section == 0 {

            if (indexPath.row == 0)||(indexPath.row == 2) {
                return true
            }
        }
        return false
    }

In this example you specify that only the first and third row of the first section of the table can be edited.

在此示例中,您指定只能编辑表的第一部分的第一行和第三行。

The second method I use is the method tableView(_: didSelectRowAtIndexPath:). In this method you specify how to react when the user selects the cell (row). In this method you can specify anything you like to change the cell. You can for instance segue to another view and do whatever you want to do. I, however, usually use an alert or action sheet to change the cell. Here is an example that extends the previous example:

我使用的第二种方法是方法tableView(_:didSelectRowAtIndexPath :)。在此方法中,您可以指定用户选择单元格(行)时的反应方式。在此方法中,您可以指定任何您想要更改单元格的内容。例如,您可以转到另一个视图并执行您想要执行的任何操作。但是,我通常使用警报或操作表来更改单元格。这是一个扩展前一个示例的示例:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //First check whether the right cell is being selected.

    let selectedIndexPath = self.trackDetailsTable.indexPathForSelectedRow

    //If the selected row is not in the first section the method returns without doing anything.
    guard selectedIndexPath?.section == 0 else {
        return
    }

    if selectedIndexPath?.row == 0 {
        //The first row is selected and here the user can change the string in an alert sheet.
        let firstRowEditAction = UIAlertController(title: "Edit Title", message: "Please edit the title", preferredStyle: .Alert)
        firstRowEditAction.addTextFieldWithConfigurationHandler({ (newTitle) -> Void in

            newTitle.text = theOldTitle //Here you put the old string in the alert text field 
        })

        //The cancel action will do nothing.
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in

            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        })

        //The Okay action will change the title that is typed in.
        let okayAction = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in

            yourObject.thatNeedsTheNewString = (firstRowEditAction.textFields?.first?.text)!
            //Do some other stuff that you want to do
            self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        })

        trackTitleEditAction.addAction(okayAction)
        trackTitleEditAction.addAction(cancelAction)
        self.presentViewController(trackTitleEditAction, animated: true, completion: nil)
    }

    if selectedIndexPath?.row == 2 {
        //The third row is selected and needs to be changed in predefined content using a standard selection action sheet.
        let trackLevelAction = UIAlertController(title: "Select Predefined Content”, message: "Please, select the content”, preferredStyle: UIAlertControllerStyle.ActionSheet)

        for content in arrayOfPredefinedContent {
            predefinedContentAction.addAction(UIAlertAction(title: content, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in

                yourObject.thatNeedsTheNewContent = content
                //Do some other stuff that you want to do
                self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
            }))

        }
        trackLevelAction.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in


        }))

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

Hope this helps.

希望这可以帮助。

#2


0  

It's a general question but here's a general answer - you link your table cell with a segue to a new scene where you can display an editable form to the user. The user clicks the table cell which triggers the segue. You might find this Apple tutorial useful.

这是一个普遍的问题,但这是一般性的答案 - 您将表格单元格与segue链接到新场景,您可以在其中向用户显示可编辑的表单。用户单击触发segue的表格单元格。您可能会发现此Apple教程很有用。

#1


1  

There are two methods that I use for editing table cells. The first method is tableView(_: canEditRowAtIndexPath indexPath:). In this method you specify the section and row of the table view that can be edited. For instance>

我使用两种方法来编辑表格单元格。第一种方法是tableView(_:canEditRowAtIndexPath indexPath :)。在此方法中,您可以指定可以编辑的表视图的节和行。例如>

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

        if indexPath.section == 0 {

            if (indexPath.row == 0)||(indexPath.row == 2) {
                return true
            }
        }
        return false
    }

In this example you specify that only the first and third row of the first section of the table can be edited.

在此示例中,您指定只能编辑表的第一部分的第一行和第三行。

The second method I use is the method tableView(_: didSelectRowAtIndexPath:). In this method you specify how to react when the user selects the cell (row). In this method you can specify anything you like to change the cell. You can for instance segue to another view and do whatever you want to do. I, however, usually use an alert or action sheet to change the cell. Here is an example that extends the previous example:

我使用的第二种方法是方法tableView(_:didSelectRowAtIndexPath :)。在此方法中,您可以指定用户选择单元格(行)时的反应方式。在此方法中,您可以指定任何您想要更改单元格的内容。例如,您可以转到另一个视图并执行您想要执行的任何操作。但是,我通常使用警报或操作表来更改单元格。这是一个扩展前一个示例的示例:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //First check whether the right cell is being selected.

    let selectedIndexPath = self.trackDetailsTable.indexPathForSelectedRow

    //If the selected row is not in the first section the method returns without doing anything.
    guard selectedIndexPath?.section == 0 else {
        return
    }

    if selectedIndexPath?.row == 0 {
        //The first row is selected and here the user can change the string in an alert sheet.
        let firstRowEditAction = UIAlertController(title: "Edit Title", message: "Please edit the title", preferredStyle: .Alert)
        firstRowEditAction.addTextFieldWithConfigurationHandler({ (newTitle) -> Void in

            newTitle.text = theOldTitle //Here you put the old string in the alert text field 
        })

        //The cancel action will do nothing.
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in

            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        })

        //The Okay action will change the title that is typed in.
        let okayAction = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in

            yourObject.thatNeedsTheNewString = (firstRowEditAction.textFields?.first?.text)!
            //Do some other stuff that you want to do
            self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        })

        trackTitleEditAction.addAction(okayAction)
        trackTitleEditAction.addAction(cancelAction)
        self.presentViewController(trackTitleEditAction, animated: true, completion: nil)
    }

    if selectedIndexPath?.row == 2 {
        //The third row is selected and needs to be changed in predefined content using a standard selection action sheet.
        let trackLevelAction = UIAlertController(title: "Select Predefined Content”, message: "Please, select the content”, preferredStyle: UIAlertControllerStyle.ActionSheet)

        for content in arrayOfPredefinedContent {
            predefinedContentAction.addAction(UIAlertAction(title: content, style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in

                yourObject.thatNeedsTheNewContent = content
                //Do some other stuff that you want to do
                self.trackDetailsTable.reloadData() //Don’t forget to reload the table view to update the table content
            }))

        }
        trackLevelAction.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in


        }))

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

Hope this helps.

希望这可以帮助。

#2


0  

It's a general question but here's a general answer - you link your table cell with a segue to a new scene where you can display an editable form to the user. The user clicks the table cell which triggers the segue. You might find this Apple tutorial useful.

这是一个普遍的问题,但这是一般性的答案 - 您将表格单元格与segue链接到新场景,您可以在其中向用户显示可编辑的表单。用户单击触发segue的表格单元格。您可能会发现此Apple教程很有用。