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

时间:2016-04-20 22:34:35

标签: swift uitableview

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

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

2 个答案:

答案 0 :(得分:1)

我用两种方法编辑表格单元格。第一种方法是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
    }

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

我使用的第二种方法是方法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)
    }
}

希望这有帮助。

答案 1 :(得分:0)

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