【发布时间】:2017-12-25 04:05:43
【问题描述】:
我正在制作一个任务共享应用程序,您可以在其中与多个人共享多个任务列表(例如与您的配偶共享一个,与您的孩子共享一个等)。每个列表的数据源都是一个名为TaskList 的结构,所以我将有多个TaskList 实例。我应该在哪里初始化这些实例?
我是这方面的新手,任何帮助将不胜感激!
这是使用 struct TaskList 创建任务列表的UITableViewController 的代码:
import UIKit
class LoLFirstTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}
@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {
if let task = AddTaskTableViewController.task {
tasks.append(task)
let indexPath = IndexPath(row: tasks.count-1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(tasks[indexPath.row].completed)
return cell
}
func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: point)
var tappedItem = tasks[indexPath!.row] as Task
tappedItem.completed = !tappedItem.completed
tasks[indexPath!.row] = tappedItem
tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
}
}
这是任务列表的代码:
import UIKit
struct TaskList {
var buddy: String
var phoneNumber: String
var tasks: [Task]
}
【问题讨论】:
标签: ios arrays swift uitableview instance-variables