【发布时间】:2016-03-21 23:53:13
【问题描述】:
我正在从字典数组 (tasks) 中加载大约 150 个元素,我可以将所有数据放入我的 tableview 中,但是当我滚动它时速度很慢。当我将我的一个函数的信息打印到控制台时,看起来每次滚动时我都会取回所有数据。这是我加载不好(即异步)还是我需要更改我的功能?
func querySections() -> [String] {
var sectionsArray = [String]()
for task in tasks {
let dueTimes = task.dueTime
sectionsArray.append(dueTimes)
}
let uniqueSectionsArray = Array(Set(sectionsArray.sort()))
// print(uniqueSectionsArray)
return uniqueSectionsArray
}
func queryDueTimes(section:Int) -> [Task] {
var sectionItems = [Task]()
for task in tasks {
let dueTimes = task.dueTime
if dueTimes == querySections()[section] {
sectionItems.append(task)
}
}
print(sectionItems)
return sectionItems
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return querySections()[section]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return querySections().count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return queryDueTimes(section).count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell
// Configure the cell...
cell.selectionStyle = .None
let times = queryDueTimes(indexPath.section)
let task = times[indexPath.row]
cell.label.text = task.title
if task.done == true {
cell.checkBox.image = UIImage(named: "checkedbox")
cell.detailLabel.text = "Completed By: \(task.completedBy)"
}
else {
cell.checkBox.image = UIImage(named: "uncheckedbox")
cell.detailLabel.text = ""
}
cell.delegate = self
return cell
}
基本上,在querySections 中,我将遍历每个任务的所有到期时间,然后将它们更改为一组数组,以便过滤掉所有重复项。这给了我所有的部分。对于queryDueTimes,我正在遍历任务并将它们匹配到一个部分。
我曾想过调用 viewDidLoad 中的函数,但这不起作用(当我尝试将它传递给另一个在函数之外更易于访问的空数组时,它一直给我一个空数组),我可以't access section (for queryDueTimes) in viewDidLoad (据我所知)。
更新 1: 我认为错误在我的最后。我说我的任务是一个数组数组,而它只是一个任务数组(一个具有每个任务所有属性的结构)。当我加载应用程序时,我将后端的所有任务附加到本地数组(“任务”)。我应该有一个数组来让它工作还是我可以以某种方式修改我的代码并让它工作?
更新 2:
当我打印 sectionTimes 和 tasksInSectionArray 时,它们是空数组。
var sectionTimes = [String]()
var tasksInSectionArray = [[Task]]()
var tasks = [Task]() {
didSet {
tableView?.reloadData()
}
}
func updateTableView() {
sectionTimes = Set(tasks.map{$0.dueTime}).sort()
tasksInSectionArray = sectionTimes.map{section in tasks.filter{$0.dueTime == section}}
print(sectionTimes)
print(tasksInSectionArray)
tableView.reloadData()
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTimes[section]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTimes.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasksInSectionArray[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell
// Configure the cell...
cell.selectionStyle = .None
let task = tasksInSectionArray[indexPath.section][indexPath.row]
【问题讨论】:
-
别猜了。不要猜测。使用仪器!它会准确地告诉您在滚动期间占用了哪些时间。然后你就会知道把注意力集中在哪里。
-
我仍然不确定如何使用它。对于使用 Swift for iOS 进行开发,我还是很陌生。你能帮我在正确的方向上提供一两步吗?
-
不幸的是,看起来 DMan 正在通过目测解决您的问题。这很糟糕,因为学习使用 Instruments 会更好! :))))
标签: ios arrays swift uitableview