如何使用关联数组数据源填充表头? Swift XCode 6.4

时间:2015-09-07 11:40:01

标签: ios xcode swift uitableview nsarray

在我的iOS应用程序中,我有一个表格,我希望显示一个包含4个类别的长任务列表:打开,关闭,暂停,过期。所以我创建了一个包含4个标题的表,每个标题都有一个唯一的标识符:

enter image description here

在教程之后,在查询数据库之后,我的任务被组织在一个对象中,其类是:

import Foundation

class 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 {
}

所以,这是与表关联的Swift类视图控制器:

import UIKit

类AllTask​​sViewController: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 {
}

实际上我对我所知道的一些微不足道感到困惑,因为我认为indexPath是一个字符串而不是一个Int ......实际上我应该这样做:

let cell = tableView.dequeueReusableCellWithIdentifier("OpenTask",
              forIndexPath: indexPath) as! UITableViewCell//2 
    cell.textLabel?.text = self.organizedTasks.items[indexPath.section][indexPath.row]

但在我的情况下,indexPath.row应该是一个String ... :(而且,如何在该关联数组中访问该状态的第n个任务对象?类似的东西

self.organizedTasks.items["Aperti"][0]

不起作用......

请你提出任何提示吗?

1 个答案:

答案 0 :(得分: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
    }
}