ios - 表格 View 中的缓慢/断断续续的滚动

标签 ios arrays swift uitableview

我正在从字典数组(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 中,我遍历每个任务的所有 dueTimes,然后将它们更改为一组数组,这样我就可以过滤掉所有重复项。这给了我所有的部分。对于 queryDueTimes,我正在遍历任务并将它们与一个部分匹配。

我曾想过调用 viewDidLoad 中的函数,但那行不通(当我尝试将它传递给另一个在函数)并且我无法访问 viewDidLoad 中的部分(对于 queryDueTimes)(据我所知该怎么做)。

更新 1: 我认为错误在我这边。我说我的任务是一个数组数组,而它只是一个任务数组(一个包含每个任务的所有属性的结构)。当我加载应用程序时,我将后端的所有任务附加到本地数组(“任务”)。我应该有一个数组来让它工作,还是我可以以某种方式修改我的代码并让它工作?

更新 2:

当我打印它们时,我将 sectionTimestasksInSectionArray 作为空数组。

    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]

最佳答案

正如您猜到的那样,数据被一遍又一遍地加载和排序,而不是只加载一次。保存 querySelectionsqueryDueTimes 的结果,并在 TableView 数据源方法中使用它。

您可以在 viewDidLoad 中执行此操作 - 调用这两个函数一次并将结果分配给类级别的变量,然后调用 tableView.reloadData()(假设你有一个 TableView 的引用)。

关于ios - 表格 View 中的缓慢/断断续续的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36143824/

相关文章:

ios - 将 Swift 文件添加到我的项目会导致无效模块错误

iOS - 为自定义键盘播放 Tock 声音

java - 要数组的多个对象

c - 无法理解以下代码中的 scanf() 行为

javascript - 自定义数组名称

ios - 无法访问不同类的变量

ios - 以编程方式创建自定义 UITableViewCell - Objective C

ios - 如何检查数组中的所有字段是否包含数据

ios - 如何使用 Kingfisher 仅在磁盘中缓存图像?

ios - UITextView .isEmpty 不适用于键盘发送 Swift