Ios 在swift的表格视图中添加滑动按钮

Ios 在swift的表格视图中添加滑动按钮,ios,swift,Ios,Swift,我有一个简单的表视图,显示任务列表。我想在用户滑动单元格时显示两个按钮。删除按钮用于删除单元格,完成按钮用于将任务存储在已完成数组中。我可以实现delete按钮,但不想在表单元格中显示第二个按钮。这是代码 import UIKit var taskArray = [String]() var datesArray = [String]() class ViewController: UIViewController, UITableViewDataSource { @IBOutlet

我有一个简单的表视图,显示任务列表。我想在用户滑动单元格时显示两个按钮。删除按钮用于删除单元格,完成按钮用于将任务存储在已完成数组中。我可以实现delete按钮,但不想在表单元格中显示第二个按钮。这是代码

import UIKit

var taskArray = [String]()
var datesArray = [String]()

class ViewController: UIViewController, UITableViewDataSource
{
    @IBOutlet weak var taskTableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return taskArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row + 1). \(taskArray[indexPath.row])"
        cell.detailTextLabel?.text = datesArray[indexPath.row]
        return cell
    }


    override func viewDidLoad()
    {
        super.viewDidLoad()
        taskTableView.dataSource = self
        let userDefaults = UserDefaults.standard
        if let task = userDefaults.stringArray(forKey: "tasks") , let date = userDefaults.stringArray(forKey: "dates")
        {
            taskArray = task
            datesArray = date
        }

        print(taskArray)
        print(datesArray)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        taskTableView.reloadData()
    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // this method handles row deletion
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
    {

        if editingStyle == .delete
        {

            // remove the item from the data model
            taskArray.remove(at: indexPath.row)
            datesArray.remove(at: indexPath.row)

            // delete the table view row
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    //function to come back from close button
    @IBAction func close(segue: UIStoryboardSegue)
    {

    }
}

Swift 4.0

您可以编写下面的
tableView
方法来定义自定义刷卡操作

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

       let delete = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in

        }
        delete.backgroundColor = UIColor.red

        let complete = UITableViewRowAction(style: .default, title: "Completed") { (action, indexPath) in
            // Do you complete operation
        }
        complete.backgroundColor = UIColor.blue

        return [delete, complete]
    }

Swift 4.0

您可以编写下面的
tableView
方法来定义自定义刷卡操作

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

       let delete = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in

        }
        delete.backgroundColor = UIColor.red

        let complete = UITableViewRowAction(style: .default, title: "Completed") { (action, indexPath) in
            // Do you complete operation
        }
        complete.backgroundColor = UIColor.blue

        return [delete, complete]
    }

首先让这个函数返回true

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
    return true
}
它使您的单元格可编辑,apple提供默认的删除和编辑选项,您可以这样使用:

或者,您可以定义自定义项:


首先让这个函数返回true

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
    return true
}
它使您的单元格可编辑,apple提供默认的删除和编辑选项,您可以这样使用:

或者,您可以定义自定义项:


Swift 4.0

  • 添加委托和数据源

    tableView.delegate = self
    tableView.dataSource = self
    
  • 添加数据源func“canEditRowAt indexPath”

  • 添加委托函数“editActionsForRowAt indexPath”


  • 我希望这能有所帮助。

    Swift 4.0

  • 添加委托和数据源

    tableView.delegate = self
    tableView.dataSource = self
    
  • 添加数据源func“canEditRowAt indexPath”

  • 添加委托函数“editActionsForRowAt indexPath”


  • 我希望这能帮上忙。

    符合你的要求。为您创建了演示

    这是输出

    按Delete键时,元素将从数组中删除,按Add键时,元素将添加到新数组中

    这是演示的链接,

    步骤1:-连接Tableview数据源并在情节提要中委派

    步骤2:-编写TableView的数据源方法

    extension ViewController: UITableViewDelegate, UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 2
        }
    
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            if section == 0 {
                return "Preloaded Data"
            } else {
                return "Added Data to New Array"
            }
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return arrPrelodedData.count
            } else {
                return arrAddNewData.count
            }
    
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard  let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeToDelete", for: indexPath) as? SwipeToDelete else {return UITableViewCell()}
            if indexPath.section == 0{
                cell.lblCellContent.text = arrPrelodedData[indexPath.row] }
            else {
                cell.lblCellContent.text = arrAddNewData[indexPath.row]
            }
    
            return cell
        }
        //With this we can edit UITableview ex. Swipe to Delete
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            if indexPath.section == 0 {
                return true } else {
                return false
            }
        }
    
        //Select tableview Editing Style (insert and Delete)-> if custom icon than set None
        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.none
        }
    
    
    
    
        //Delete Action 1) Create delete Action 2) Remove data with Indexpath 3) fetch data from coredata 4) delete tableview row 4) set delete button background color 5) return deleteAction in arry wether it is single
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            //Destructive Because we want to delete(destroy) the data from tableview
            let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexpath) in
                self.arrPrelodedData.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexpath], with: .automatic)
            }
    
            let addAction = UITableViewRowAction(style: .normal, title: "ADD 1") { (rowAction, indexpath) in
                self.arrAddNewData.append(self.arrPrelodedData[indexPath.row])
                tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
    
    
               // tableView.reloadRows(at: [indexPath], with: .automatic)
    
            }
            deleteAction.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
            addAction.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.662745098, blue: 0.2666666667, alpha: 1)
            return [deleteAction,addAction]
    }
    }
    

    我希望这个答案对你有帮助。

    符合你的要求。为您创建了演示

    这是输出

    按Delete键时,元素将从数组中删除,按Add键时,元素将添加到新数组中

    这是演示的链接,

    步骤1:-连接Tableview数据源并在情节提要中委派

    步骤2:-编写TableView的数据源方法

    extension ViewController: UITableViewDelegate, UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 2
        }
    
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            if section == 0 {
                return "Preloaded Data"
            } else {
                return "Added Data to New Array"
            }
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return arrPrelodedData.count
            } else {
                return arrAddNewData.count
            }
    
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard  let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeToDelete", for: indexPath) as? SwipeToDelete else {return UITableViewCell()}
            if indexPath.section == 0{
                cell.lblCellContent.text = arrPrelodedData[indexPath.row] }
            else {
                cell.lblCellContent.text = arrAddNewData[indexPath.row]
            }
    
            return cell
        }
        //With this we can edit UITableview ex. Swipe to Delete
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            if indexPath.section == 0 {
                return true } else {
                return false
            }
        }
    
        //Select tableview Editing Style (insert and Delete)-> if custom icon than set None
        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.none
        }
    
    
    
    
        //Delete Action 1) Create delete Action 2) Remove data with Indexpath 3) fetch data from coredata 4) delete tableview row 4) set delete button background color 5) return deleteAction in arry wether it is single
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            //Destructive Because we want to delete(destroy) the data from tableview
            let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexpath) in
                self.arrPrelodedData.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexpath], with: .automatic)
            }
    
            let addAction = UITableViewRowAction(style: .normal, title: "ADD 1") { (rowAction, indexpath) in
                self.arrAddNewData.append(self.arrPrelodedData[indexPath.row])
                tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
    
    
               // tableView.reloadRows(at: [indexPath], with: .automatic)
    
            }
            deleteAction.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
            addAction.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.662745098, blue: 0.2666666667, alpha: 1)
            return [deleteAction,addAction]
    }
    }
    

    我希望这个答案对您有所帮助。

    我在右击时也做了同样的删除按钮。但我想让这个删除按钮的角半径为6。但没能成功。有什么建议吗?我在右击时做了同样的删除按钮。但我想让这个删除按钮的角半径为6。但没能成功。有什么建议吗?
    extension ViewController: UITableViewDelegate, UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 2
        }
    
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            if section == 0 {
                return "Preloaded Data"
            } else {
                return "Added Data to New Array"
            }
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return arrPrelodedData.count
            } else {
                return arrAddNewData.count
            }
    
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard  let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeToDelete", for: indexPath) as? SwipeToDelete else {return UITableViewCell()}
            if indexPath.section == 0{
                cell.lblCellContent.text = arrPrelodedData[indexPath.row] }
            else {
                cell.lblCellContent.text = arrAddNewData[indexPath.row]
            }
    
            return cell
        }
        //With this we can edit UITableview ex. Swipe to Delete
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            if indexPath.section == 0 {
                return true } else {
                return false
            }
        }
    
        //Select tableview Editing Style (insert and Delete)-> if custom icon than set None
        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.none
        }
    
    
    
    
        //Delete Action 1) Create delete Action 2) Remove data with Indexpath 3) fetch data from coredata 4) delete tableview row 4) set delete button background color 5) return deleteAction in arry wether it is single
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            //Destructive Because we want to delete(destroy) the data from tableview
            let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexpath) in
                self.arrPrelodedData.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexpath], with: .automatic)
            }
    
            let addAction = UITableViewRowAction(style: .normal, title: "ADD 1") { (rowAction, indexpath) in
                self.arrAddNewData.append(self.arrPrelodedData[indexPath.row])
                tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .none)
    
    
               // tableView.reloadRows(at: [indexPath], with: .automatic)
    
            }
            deleteAction.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
            addAction.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.662745098, blue: 0.2666666667, alpha: 1)
            return [deleteAction,addAction]
    }
    }