我正在从字典数组(tasks)的数组中加载大约150个元素,并且可以将所有数据放入表视图中,但是当我滚动其愚蠢的速度时。当我将其中一项功能的信息打印到控制台时,似乎每次滚动都将所有数据取回。这是我无法很好地加载(即异步加载)的东西吗?还是需要更改功能?

    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中,我要遍历每个任务的所有DueTime,然后将它们更改为集合的数组,以便可以过滤出所有重复项。这给了我所有的部分。对于queryDueTimes,我要遍历任务并将它们匹配到一个部分。

我曾想过要在viewDidLoad中调用函数,但这种方法不起作用(当我尝试将其传递给该函数外部更容易访问的另一个空数组时,它一直给我一个空数组),而且我无法访问该部分( (对于queryDueTimes)在viewDidLoad中(据我所知)。

更新1:
我认为错误就在我身边。我说过,当task只是一个Tasks数组(具有每个任务的所有属性的结构)时,tasks是一个数组数组。加载应用程序时,我会将后端的所有任务附加到本地数组(“任务”)。我应该有一个可以工作的数组数组,还是可以以某种方式修改代码并使它正常工作?

更新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]

最佳答案

var sections: [String] = []
var data: [[Tasks]] = []

func updateTableView() {
    sections = Set(tasks.map { $0.dueTime }).sort()
    data = sections.map { section in tasks.filter { $0.dueTime == section } }
    tableView.reloadData()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data[section].count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let task = data[indexPath.section][indexPath.row]

    // Cell configuration
}

这基本上就是DMan所说的,但是我为您提供了一个例子。

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

10-14 11:16