【问题标题】:Swift calling a class from a view controllerSwift 从视图控制器调用一个类
【发布时间】: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


【解决方案1】:

您将在 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
    }


}

【讨论】:

    【解决方案2】:

    首先不需要创建TaskCollection 类,因为它的功能已经内置在数组中,所以你需要创建一个任务数组

     var tasksArr = [Task]()
    

    //

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
      return self.tasksArr.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.tasksArr[indexPath.row].description 
    
       return cell
    }
    

    OR 根据当前实现

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      相关资源
      最近更新 更多