ios - 在 Swift 中计算数据时如何改进 UITableView 滚动?

标签 ios swift uitableview

在改变我们在 cellForRow: 方法中返回单元格的方式之前,我记得我可以检查 if cell == nil,并且基本上有两个独立的与单元格相关的自定义区域(适用于所有单元格的属性与仅适用于某些单元格的属性)。但是对于 dequeueReusableCell:,我不确定如何处理这个问题并推迟它,直到我能够正确填充我的所有单元格——但似乎以大量 CPU 和内存使用为代价。这是我目前在 cellForRow: 中的代码:

    if case (1...(days.count + 1), 2...(hours.count + 2)) = (indexPath.column, indexPath.row) {

            let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: ScheduleCell.self), for: indexPath) as! ScheduleCell

            let legs : NSArray = importedDict.object(forKey: "legs") as! NSArray

            print(hours![indexPath.row - 2])

            var profits : Array<Double> = []

            if let singleLegDict2 = legs as? [[String:AnyObject]] {

                print (singleLegDict2)
                for singleLegDict in singleLegDict2 {
                    let dict = singleLegDict as NSDictionary

                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd"
                    let datee = dateFormatter.date(from: days![indexPath.column - 1] as! String)!
                    let datee2 = dateFormatter.date(from: dict.object(forKey: "expiry") as! String)!

                    let calendar = Calendar.current

                    // Replace the hour (time) of both dates with 00:00
                    let date1 = calendar.startOfDay(for: datee)
                    let date2 = calendar.startOfDay(for: datee2)

                    let components = calendar.dateComponents([.day], from: date1, to: date2)
                    let doub = Double(components.day!)

                    let years = doub * 0.00273972602

//                    let under : NSNumber = dict.object(forKey: "underlying") as! NSNumber
                    let strike : NSNumber = dict.object(forKey: "strikePrice") as! NSNumber
                    let interest1 : NSNumber = dict.object(forKey: "interest") as! NSNumber
                    var interest2 = interest1 as! Double
                    interest2 = interest2/100
                    let userIV : NSNumber = NSNumber(value: Double(dict.object(forKey: "userIV") as! String)!)
                    var userIV2 = userIV as! Double
                    userIV2 = userIV2/100

                    var type = 0
                    if dict.object(forKey: "putCall") as! String == "CALL" {
                        type = 0
                    } else {
                        type = 1
                    }

                    let theo = getTheoreticalOptionPrice(forUnderlyingPrice: hours![indexPath.row - 2] as! NSNumber, withStrike: strike, withInterestRate: NSNumber(value: interest2), withVolatility: NSNumber(value: userIV2), withTimeToExpirationInYears: NSNumber(value: years), withType: NSNumber(value: type))
                    let userC : NSNumber = NSNumber(value: Double(dict.object(forKey: "userCost") as! String)!)
                    let quan : NSNumber = dict.object(forKey: "quantity") as! NSNumber
                    let diff = (theo - (userC as! Double)) * (quan as! Double)
                    print(theo, (userC as! Double))
                    profits.append(diff)
                }

            }

            print(profits)

            var sumedArr : Double = profits.reduce(0, {$0 + $1}) as Double
            sumedArr = sumedArr * 100

            if sumedArr < 0 {
                cell.backgroundColor = UIColor(red: 0.8667, green: 0.1804, blue: 0.1804, alpha: 1.0)

            } else {
                cell.backgroundColor = UIColor(red: 0.3843, green: 0.7686, blue: 0.1608, alpha: 1.0)
            }

            cell.label.text = String(format: "%.0f", sumedArr)

            return cell
        }

关于如何加快速度的任何想法?唯一的方法是预加载数据,然后仅在 cellForRow: 中引用它吗?考虑到我对每个单元格进行的计算,我将如何解决这种情况?

我不需要每次滚动到单元格时都重新加载它们的数据。如果所有单元格在 View 加载时都加载一次数据,并且数据刚刚显示——那就完美了。

考虑到这一点,我尝试添加一个检查以查看单元格的标签是否已填充,如果没有,则进行所有这些计算(假设这将是表格 View 的第一次加载)。虽然这提高了滚动性能,但单元格没有正确显示并且完全乱序。帮忙?

更新:我已将繁重的计算移至后台线程,现在可以体验流畅的滚动。但是,单元格仍然闪烁,因为它们在每次显示时仍在重新生成数据。缓存单元格的最简单方法是什么,以便它们在显示 tableView 时加载一次,然后因为它们引用已加载的数据而平滑滚动?

最佳答案

我不明白为什么了解 tableView 单元格是否已被新初始化或被重新使用对您的应用程序的性能如此重要。但如果是这样,你可以像以前一样处理它。 docs说:

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its init(style:reuseIdentifier:) method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse() method instead.

所以你应该能够区分这两种情况并做出相应的回应。

关于ios - 在 Swift 中计算数据时如何改进 UITableView 滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56778596/

相关文章:

iOS:搜索框的语音转文本

ios - 向上缩放 UIView

ios - 带有动态高度单元和动态内容的表格 View 单元格内的 Collection View swift 4

ios - Interface Builder 在每次操作后水平折叠垂直堆栈 View 并返回

ios - 在 swift 项目中从 Objective-C 调用 Swift 函数

swift - NSCoding 和 GameplayKit 类

json - 如何实现 TableView 中显示的json的搜索操作

ios - 在 Spritekit 中使用 UITableView

ios - 如何通过比较swift中的每个字符来排序?

ios - 如何在准备中的 viewController 之间通过引用传递快速数组(对于 segue :?