Swift从视图控制器调用类
我是来自爪哇的swift初学者。我有一个task类和一个taskcollection类,它是任务的集合。我正在尝试将集合放入表视图中,但我不知道如何操作 这是任务类:Swift从视图控制器调用类,swift,xcode,uitableview,class,Swift,Xcode,Uitableview,Class,我是来自爪哇的swift初学者。我有一个task类和一个taskcollection类,它是任务的集合。我正在尝试将集合放入表视图中,但我不知道如何操作 这是任务类: class Task{ private var description : String init(description: String){ self.description = description } func getDescription() -> String{ return self.des
class Task{
private var description : String
init(description: String){
self.description = description
}
func getDescription() -> String{
return self.description
}
func setDescription(description: String){
self.description = description
}}
这是taskCollection类:
class TaskCollection{
private var tasks: Array<Task>
init(){
self.tasks = [Task]()
}
func getTasks() -> Array<Task>{
return self.tasks
}
func addTask(task: Task){
self.tasks.append(task)
}}
首先,不需要创建一个
TaskCollection
类,因为它的功能已经内置在数组中,所以需要创建一个任务数组
var tasksArr = [Task]()
//
或者根据目前的执行情况
var colc = TaskCollection()
//
您将在ViewController文件范围中的某个位置创建TaskCollection类的实例。然后在其他地方填充它(我这样做了,以viewDidLoad为例)。行数就是taskCollection实例中的任务数。您可以通过使用indexPath.row获取任务数组的索引来访问每个任务
class TableViewController: UITableViewController {
let taskCollection = TaskCollection()
override func viewDidLoad() {
super.viewDidLoad()
let task = Task(description: "Some Description")
taskCollection.addTask(task: task)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskCollection.getTasks().count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let task = taskCollection.getTasks()[indexPath.row]
cell.textLabel?.text = task.getDescription()
return cell
}
}
关于Task类的旁注。您可以将描述设置为
private(set)var description:String
,以使其从类本身外部只读
var colc = TaskCollection()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.colc.tasks.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableviewTasks.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// set the text from the data model
cell.textLabel?.text = self.colc.tasks[indexPath.row].description
return cell
}
class TableViewController: UITableViewController {
let taskCollection = TaskCollection()
override func viewDidLoad() {
super.viewDidLoad()
let task = Task(description: "Some Description")
taskCollection.addTask(task: task)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskCollection.getTasks().count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let task = taskCollection.getTasks()[indexPath.row]
cell.textLabel?.text = task.getDescription()
return cell
}
}