嗨,我正在尝试创建一个待办事项列表应用程序,以在表视图中显示所有待办事项。现在,我可以向tableView添加任务列表。并通过向左滑动将其删除。删除每一行时,我只能看到“删除”选项。但我想添加更多选项。我该如何添加。我还需要一个称为“已完成”的选项。任何帮助表示赞赏。这是下面的代码。
//
// ViewController.swift
// MiniTasks
//
// Created by Reddy on 19/7/2018.
// Copyright © 2016 Reddy. All rights reserved.
//
import UIKit
// Creation of empty arrays
var taskArray = [String]()
var datesArray = [String]()
var completedArray = [String]()
var deletedArray = [String]()
class ViewController: UIViewController, UITableViewDataSource {
// IBOutlets
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var addButton: UIButton!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Refresing table view.
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Setting dataSource to self.
tableView.dataSource = self
addButton.layer.cornerRadius = 25
addButton.clipsToBounds = true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath)
// (indexPath.row + 1) shows the row count.
cell.textLabel?.text = "\(indexPath.row + 1).\(taskArray[indexPath.row])"
cell.detailTextLabel?.text = datesArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
// delete data and row
taskArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArray.count
}
@IBAction func close(segue: UIStoryboardSegue)
{
}
}
.......................................................................
//
// AddTaskViewController.swift
// MiniTasks
//
// Created by Reddy on 19/7/2018.
// Copyright © 2016 Appkoder. All rights reserved.
//
import UIKit
// Defined UIPickerViewDelegate, and UIPickerViewDataSource Protocols.
class AddTaskViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// IBOutlets
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var addTaskButton: UIButton!
@IBOutlet weak var datePicker: UIDatePicker!
var selectedTask = ""
// Array
var tasks = ["Select task", "Go to dentist", "Meet friends", "Iron my clothes", "Call parents", "Pay bills", "Paint desk", "Call insurance company", "Cook dinner", "Buy gifts for wife", "Take kids out", "Go for swimming lesson", "Play tennis", "Clean the house", "Track package"]
// DateFormatter Instance creation.
var dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
// DataSource and Delegate
pickerView.dataSource = self
pickerView.delegate = self
addTaskButton.isEnabled = false
datePicker.minimumDate = Date()
dateFormatter.dateFormat = "EE dd-MMM-yyyy HH:mm"
addTaskButton.layer.cornerRadius = 5
addTaskButton.layer.borderColor = UIColor.white.cgColor
addTaskButton.layer.borderWidth = 1.0
addTaskButton.clipsToBounds = true
}
// PickerView Delegate methods.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return tasks.count
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return tasks[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0
{
addTaskButton.isEnabled = false
}
else
{
addTaskButton.isEnabled = true
selectedTask = tasks[row]
}
}
@IBAction func addTask(_ sender: AnyObject) {
let date = datePicker.date
let dateStr = dateFormatter.string(from: date)
taskArray.append(selectedTask)
datesArray.append(dateStr)
dismiss(animated: true, completion: nil)
}
}