我正在编写一个任务应用程序,我正在尝试从用户那里收集输入,并使用它来显示更新的UITableView和用户创建的任务。
TaskManager.swift
类接收任务详细信息并向数组添加任务结构。
import UIKit
struct task{
var name: String
var desc: String
var due_date: String
init(name: String, desc: String, due_date: String)
{
self.name = name
self.desc = desc
self.due_date = due_date
}
}
var TaskMgr: TaskManager = TaskManager()
class TaskManager: UIViewController {
var tasks = [task]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
* Purpose: Add new task to task array
* Parameters: task (struct) to add
* Return: n/a
*/
func addTask(name: String, desc: String, due_date: String) {
print("got to add task method")
tasks.append(task(name: name, desc: desc, due_date: due_date))
print("value of tasks[0] is:",tasks[0])
print("number of elements in array: ",tasks.count)
//the task array count should increase as the user adds more tasks,
//but from this code, it is replacing elements
//(only saying there is one element, the most recently added one)
}
这是FirstViewController.swift
类,其中输入从新任务屏幕读取,并发送到TaskManager类以添加到任务列表。
import UIKit
//display task class
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var taskTable: UITableView!
@IBOutlet weak var taskName_txt: UITextField!
@IBOutlet weak var taskDesc_txt: UITextField!
@IBOutlet weak var taskDueDate_txt: UIDatePicker!
@IBOutlet weak var taskDone_btn: UIBarButtonItem!
@IBOutlet weak var addTask_btn: UIBarButtonItem!
var activeTextField = UITextField()
//Cancel task button function
@IBAction func cancelTask_btn(_ sender: Any) {
performSegue(withIdentifier: "backToTasks_seg", sender: cancelTask_btn)
}
override func viewDidLoad() {
super.viewDidLoad()
//taskTable.reloadData()
//taskTable.reloadData()
}
@IBAction func addTaskPressed(_ sender: Any) {
performSegue(withIdentifier: "addTask_seg", sender: taskDone_btn)
}
func textFieldDidBeginEditing(textField: UITextField) {
self.activeTextField = textField
activeTextField.backgroundColor = UIColor(red: 187.00, green: 128.00, blue: 74.00, alpha: 1.00)
}
/*
* Purpose: Triggered off user 'Done' button click
Creates a task to add
* Parameters: button clicked
* Return: n/a
*/
@IBAction func doneTaskInput_btn(_ sender: Any) {
print("Done task input button pressed")
//if user input is valid, create a task from data they input
//return back to task table screen
if(isInputValid() != false){
let name = taskName_txt.text!
print("Task name is: ",name)
let desc = taskDesc_txt.text!
print("Task desc is: ",desc)
let due_date = handler(sender: taskDueDate_txt)
print("Task due date is: ",due_date)
print("Adding task to task list...")
TaskManager().addTask(name: name, desc: desc, due_date: due_date)
}//end check isValid
//added task to array, now go back to task table to display tasks
performSegue(withIdentifier: "backToTasks_seg", sender: taskDone_btn)
}//end function
/*
* Purpose: checks if user entered a task name
* Parameters: none
* Return: true if user entered task name
false otherwise
*/
func isInputValid()-> Bool{
var taskName: String?
if taskName != ""{
taskName = taskName_txt.text
return true
}else{
taskName_txt.backgroundColor = UIColor.red
taskName_txt.placeholder = "Add a task name"
return false
}
}//end isInputValid method
/*
* Purpose: Parses date from UIDatePicker to string
* Parameters: DatePicker to read date from
* Return: formatted string date
*/
func handler(sender: UIDatePicker)->String {
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let strDate = timeFormatter.string(from: taskDueDate_txt.date)
return strDate
}//end handler method
/*
* Purpose: Remove cell
* Parameters: task table, edit value
* Return: n/a
*/
func tableView(taskTable: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.delete){
TaskMgr.tasks.remove(at: indexPath.row)
taskTable.reloadData()
}
}
/*
* Purpose: Returns number of sections (columns) in table
* Parameters: table
* Return: number of sections (colums)
*/
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
print( "setting number of sections");
return 1
}
/*
* Purpose: Returns number of rows in array to the table view
* Parameters: taskTable, number of rows
* Return: number of rows (int)
*/
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TaskMgr.tasks.count // taskList.count
}
/*
* Purpose: Populate cells with data from array of task structs
* Parameters: task table, row to insert data
* Return: populated cell
*/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Default Tasks")
cell.textLabel?.text = TaskMgr.tasks[indexPath.row].name
cell.detailTextLabel!.text = TaskMgr.tasks[indexPath.row].desc
return cell
}
}
感谢您的帮助!
答案 0 :(得分:0)
除非您使用像Rx这样的东西,否则iOS中没有数据绑定。这意味着如果您更改了您负责的数据,请告诉tableView使用reloadData或insertRows(at:with:)更新自己。
有一个问题,你想在哪里做到这一点。在简单的情况下,您可以在添加后立即执行此操作。在更复杂的情况下,您订阅来自TaskManager的通知,并且taskmanager会在数据发生变化时发送您的视图控制器消息(这使您的数据保持同步,这是多个视图控制器,我假设您有一个单独的TaskManager)。
并不是说你已经在删除了。
答案 1 :(得分:0)
每次你看到任务管理器视图控制器,它都是一个新实例时,我有一种感觉吗?
var tasks = [task]()
这可能也会重置它? 我尝试将tasks数组放在FirstViewController中,然后你可以直接访问它里面的结构。