gpt4 book ai didi

ios - 快速在表格 View 中添加滑动按钮

转载 作者:行者123 更新时间:2023-11-28 11:49:07 24 4
gpt4 key购买 nike

我有一个显示任务列表的简单表格 View 。当用户在单元格上滑动时,我想显示两个按钮。用于删除单元格的删除按钮和用于将任务存储在已完成数组中的已完成按钮。我能够实现删除按钮,但不知道在表格单元格中显示第二个按钮。这是代码。

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]
}

关于ios - 快速在表格 View 中添加滑动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com