gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:58:35 25 4
gpt4 key购买 nike

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

enter image description here

按照教程,在查询数据库后,我的任务被组织在一个对象中,该对象的类是这样的:

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

所以,这是我关联到表的 Swift 类 View Controller :

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 应该是一个字符串... :( 此外,我如何访问该关联数组中该状态的第 n 个任务对象?类似的东西

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

不会工作...

你能给点提示吗?

最佳答案

在swift中你可以使用这个方法来显示header

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

关于ios - 如何使用关联数组数据源填充表头? swift XCode 6.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com