【发布时间】:2018-03-24 18:09:31
【问题描述】:
我是来自 java 的 swift 初学者。我有一个任务类和一个任务集合类,它是任务的集合。我正在尝试将集合放入表格视图中,但我不知道如何。
这是任务类:
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 放入 tableview 我该怎么做?在 java 中,我知道我必须制作类似 new TaskCollection() 的东西,但我不知道如何在 tableviews 中获取它:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ?
}
// 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.___?_____[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
【问题讨论】:
-
Task 类的旁注。您可以将描述设置为
private(set) var description: String以使其在类本身外部只读
标签: swift xcode uitableview class