Ios 如何使用具有关联数组数据源的标题填充表?Swift XCode 6.4

Ios 如何使用具有关联数组数据源的标题填充表?Swift XCode 6.4,ios,xcode,swift,uitableview,nsarray,Ios,Xcode,Swift,Uitableview,Nsarray,在我的iOS应用程序中,我有一个表格,我想在其中显示一长串任务,包括4个类别:打开、关闭、暂停、过期。因此,我创建了一个包含4个标题的表,每个标题都有其单元格的唯一标识符: 根据教程,查询数据库后,我的任务将组织在一个对象中,该对象的类为: import Foundation 类TaskMenuItems:NSObject{ var sections:[String] = [] var items:[String:[Task]] = [:] func addSection(section:

在我的iOS应用程序中,我有一个表格,我想在其中显示一长串任务,包括4个类别:打开、关闭、暂停、过期。因此,我创建了一个包含4个标题的表,每个标题都有其单元格的唯一标识符:

根据教程,查询数据库后,我的任务将组织在一个对象中,该对象的类为:

import Foundation
类TaskMenuItems:NSObject{

var sections:[String] = []
var items:[String:[Task]] = [:]

func addSection(section:String, sectionItems:[Task]) {
    self.sections = self.sections + [section]
    self.items[section] = sectionItems
}

func organize(tasks:[Task]) {

    var open:[Task] = []
    var closed:[Task] = []
    var expired:[Task] = []
    var suspended:[Task] = []

    for task in tasks {
        switch task.stato {
        case "Aperto":
            open.append(task)
            break
        case "Chiuso":
            closed.append(task)
            break
        case "Scaduto":
            expired.append(task)
            break
        case "Sospeso":
            suspended.append(task)
            break
        default:
            break

        }
    }

    self.addSection("Aperti", sectionItems: open)
    self.addSection("Chiusi", sectionItems: closed)
    self.addSection("Scaduti", sectionItems: expired)
    self.addSection("Sospesi", sectionItems: suspended)
}
}
因此,它是一个关联数组,其中键是任务的状态,而值是具有该状态的任务数组。这是正确完成的…问题是我无法编写以下方法来填充表:

override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
import UIKit
因此,下面是与表关联的Swift类视图控制器:

override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
import UIKit
类AllTasksViewController:UITableViewController{

var allTasks = [Task]()
var taskService = TaskService()
var organizedTasks = TaskMenuItems()

override func viewWillAppear(animated: Bool) {
    self.tabBarController?.navigationItem.setHidesBackButton(true, animated: true)
     self.tabBarController?.navigationItem.rightBarButtonItem = self.editButtonItem()
    if (LoggedUser.isLogged) {
        self.navigationItem.setHidesBackButton(false, animated: true)
        self.taskService.requestAllTasks {
            (response) in
            self.allTasks = self.taskService.loadTasks(response) as! [Task]
            self.organizedTasks.organize(self.allTasks)
            /*println(self.organizedTasks.items["Aperti"]?.count)
            println(self.organizedTasks.items["Chiusi"]?.count)
            println(self.organizedTasks.items["Scaduti"]?.count)
            println(self.organizedTasks.items["Sospesi"]?.count)*/
            dispatch_async(dispatch_get_main_queue()) {
                self.tableView.reloadData()
            }
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


/* Number of sections */
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.organizedTasks.sections.count
}

/* Number of rows for each section */
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch section {
    case 0:
        return self.organizedTasks.items["Aperti"]!.count
    case 1:
        return self.organizedTasks.items["Chiusi"]!.count
    case 2:
        return self.organizedTasks.items["Scaduti"]!.count
    case 3:
        return self.organizedTasks.items["Sospesi"]!.count
    default:
        return -1
    }

}


override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
事实上,我对一些琐碎的事情感到困惑,因为我认为将XPath作为字符串而不是Int进行索引……事实上,我应该这样做:

let cell = tableView.dequeueReusableCellWithIdentifier("OpenTask",
              forIndexPath: indexPath) as! UITableViewCell//2 
    cell.textLabel?.text = self.organizedTasks.items[indexPath.section][indexPath.row]
但在我的例子中,indexath.row应该是一个字符串…:(此外,我如何访问关联数组中该状态的第n个任务对象?类似的东西

self.organizedTasks.items["Aperti"][0]
不起作用


您能给出一些提示吗?

在swift中,您可以使用此方法显示标题

 func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
 /*   if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)

        let subview = UIView(frame: CGRect(x: 0, y: view.frame.height - 2, width: view.frame.width, height: 2))
        subview.backgroundColor = UIColor.whiteColor()
        view.addSubview(subview) //according to need
    }*/
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    if(section == 0)
    {
        return " Section 0"// according to need
    }
    else
    {
        return " Section 1"// according to need oR make it generic
    }
}

在swift中,您可以使用此方法显示标题

 func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
 /*   if let view = view as? UITableViewHeaderFooterView {
        view.backgroundView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)

        let subview = UIView(frame: CGRect(x: 0, y: view.frame.height - 2, width: view.frame.width, height: 2))
        subview.backgroundColor = UIColor.whiteColor()
        view.addSubview(subview) //according to need
    }*/
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    if(section == 0)
    {
        return " Section 0"// according to need
    }
    else
    {
        return " Section 1"// according to need oR make it generic
    }
}

谢谢,但我的问题不是显示标题,而是用以下方法填充单元格:override func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell{ehm谢谢,但我的问题不是显示标题,而是用以下方法填充单元格:override func tableView(tableView:UITableView,cellForRowAtIndexPath-indexath:nsindepath)->UITableViewCell{