2017-06-12 174 views
1

我有一个非常奇怪的问题,那就是当重新渲染NSTableView时单元格看起来会随机着色并显示文本。这是MacOS不是iPhone。NSTableView Table View Cell Memory问题

我使用变量数据中的字典将结果显示到表中。

最简单的方法来显示它看起来应该是这样的... enter image description here

当我再次运行模拟,并删除所有表列并重新创建它们赶上了“年”不是几个月的,那么它看起来像这样。 enter image description here

这是一个非常奇怪的错误。我不知道我在做什么错误,可能会造成这种情况,但我怀疑我正在做的事情有内存问题。

这是我的控制器代码。

import Cocoa 

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource { 

    //@JA - Simulation Settings Tab 
    @IBOutlet weak var theTableview: NSTableView! 
    @IBOutlet weak var reportTypePopUpButton: NSPopUpButton! 
    @IBOutlet weak var businessPopUpButton: NSPopUpButton! 
    @IBOutlet weak var numberOfDaysToSimulateTextField: NSTextField! 
    @IBOutlet weak var startDateTextField: NSTextField! 
    @IBOutlet weak var startingBudgetTextField: NSTextField! 

    //Default Multidimensional Dictionary 
    var data = [ 
     [ 
      "name":"Click Start to Begin", 
      "columninfo" :["0"] 
     ] 
    ] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //First remove all columns 
     let columns = self.theTableview.tableColumns 
     columns.forEach { 
      self.theTableview.removeTableColumn($0) 
     } 

     //@JA - This fixes the last column not being shown correctly 
     self.theTableview?.columnAutoresizingStyle = .noColumnAutoresizing 

     for index in 0...1 { 
      let column = NSTableColumn(identifier: "defaultheader") 
      if(index != 0){ 
       column.title = "Month \(index)" 
      }else{ 
       column.title = "Factors" 
      } 

      self.theTableview.addTableColumn(column) 
     } 


     let date = NSDate() 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "yyyy-MM-dd" 
     let dateString = dateFormatter.string(from:date as Date) 

     startDateTextField.stringValue = dateString 

     // Do any additional setup after loading the view. 

     self.theTableview.reloadData() 
    } 

    override var representedObject: Any? { 
     didSet { 
     // Update the view, if already loaded. 
     } 
    } 

    //@JA - Tableview Delegate & Datasource Functions 
    func numberOfRows(in tableView: NSTableView) -> Int { 
     return data.count 
    } 

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { 

     let currentColumnIndex = tableView.tableColumns.index(of: tableColumn!) 

     if let cell = tableView.make(withIdentifier: "defaultcell", owner: nil) as? NSTableCellView { 
     //if let cell = tableView.reuse { 
      if tableColumn == tableView.tableColumns[0]{ //@JA - If this is the first column then show the row names corespondingly 
       cell.textField?.stringValue = data[row]["name"] as! String 
      }else{ 
       let columnInfo = data[row]["columninfo"] as! [String] 

       let isIndexValid = columnInfo.indices.contains(currentColumnIndex!-1) 
       if(isIndexValid){ 
        cell.textField?.stringValue = columnInfo[currentColumnIndex!-1] 
       } 
      } 

      //Highlight Rows with rule to do so 
      if data[row]["highlightrow"] != nil{ 
       let bghighlight = data[row]["highlightrow"] as! [String:NSNumber] 
       cell.textField?.backgroundColor = NSColor.init(red: CGFloat(bghighlight["red"]!), green: CGFloat(bghighlight["green"]!), blue: CGFloat(bghighlight["blue"]!), alpha: CGFloat(bghighlight["alpha"]!)) 
       cell.textField?.textColor = NSColor.init(red: CGFloat(bghighlight["tred"]!), green: CGFloat(bghighlight["tgreen"]!), blue: CGFloat(bghighlight["tblue"]!), alpha: CGFloat(bghighlight["talpha"]!)) 
      } 

      //Marked rows will show ----- for content to fill it out 
      if data[row]["mark"] != nil{ 
       if data[row]["mark"] as! Bool == true && currentColumnIndex != 0{ 
        cell.textField?.stringValue = "---------------" 
       } 
      } 

      return cell 
     } 
     return nil 
    } 

    //@JA - Helper Functions 
    func calculateTableHeaders(){ 

     //First remove all columns 
     let columns = self.theTableview.tableColumns 
     columns.forEach { 
      self.theTableview.removeTableColumn($0) 
     } 

     //Find out how many days we want to simulate 
     let numOfDays = Int(numberOfDaysToSimulateTextField.intValue) 
     let columnType = reportTypePopUpButton.titleOfSelectedItem 

     //Generic Setting for Date 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "yyyy-MM-dd" 

     let startDate = dateFormatter.date(from:startDateTextField.stringValue) 
     var components = DateComponents() 

     var numOfColumns = 150 //Default Column Setting. Always overwritten 

     print("columnType=\(columnType!)") 

     if(columnType == "Daily"){ 
      numOfColumns = Int(numOfDays) 
      components = DateComponents() //to reset it 
     } 
     if(columnType == "Monthly"){ 
      components.setValue(numOfDays, for: .day) 
      let futureDate = Calendar.current.date(byAdding: components, to: startDate!) 
      numOfColumns = futureDate!.interval(ofComponent: .month, fromDate: startDate!) //The number of months needed column wise 
     } 
     if(columnType == "Yearly"){ 
      components.setValue(numOfDays, for: .day) 
      let futureDate = Calendar.current.date(byAdding: components, to: startDate!) 
      numOfColumns = futureDate!.interval(ofComponent: .year, fromDate: startDate!) //The number of months needed column wise 
     } 

     dateFormatter.dateFormat = "dd MMM yyyy" //this is format we want to use for column names 
     for index in 0...numOfColumns { 
      let column = NSTableColumn(identifier: "defaultheader") 
      if(index != 0){ 
       components = DateComponents() //reset it 
       if(columnType == "Daily"){ 
        components.setValue(index, for: .day) 
       }else if(columnType == "Monthly"){ 
        components.setValue(index, for: .month) 
       }else if(columnType == "Yearly"){ 
        components.setValue(index, for: .year) 
       } 
       let dayDate = Calendar.current.date(byAdding: components, to: startDate!) 
       column.title = dateFormatter.string(from:dayDate!) 
      }else{ 
       column.title = "Factors" 
       column.width = 200 
      } 

      self.theTableview.addTableColumn(column) 
     } 

    } 

    @IBAction func startsimulation(_ sender: NSButton) { 
     calculateTableHeaders() 
     let budget = startingBudgetTextField.doubleValue 
     let businessname = businessPopUpButton.titleOfSelectedItem! 

     var biz:Business? 

     if (businessname == "PoolService123.com"){ 
      biz = PoolService123(bizname: businessname, startbudget:Decimal(budget)) 
     } 

     let sim = Simulator(biz: biz!) 

     data = [] 
     data = sim.run() as! [Dictionary<String, Any>] 


     theTableview.reloadData() 
    } 

} 

extension Date { 

    func interval(ofComponent comp: Calendar.Component, fromDate date: Date) -> Int { 

     let currentCalendar = Calendar.current 

     guard let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 } 
     guard let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 } 

     return end - start 
    } 
} 

相关类公司根据我们的定义,像这样:

Simulator.swift

import Foundation 


class Simulator { 

    var business: Business 
    var daysToSimulate = 365 

    //Must be initialized with a business 
    init(biz: Business) { 
     self.business = biz 
    } 

    public func run() -> Any{ 
     let data = self.business.data! //Grabs the default data structure from which to fill in details 

     return data 
    } 
} 

Business.swift

import Foundation 

class Business { 
    public var name: String = "" 
    public var budget:Decimal = 0.0 
    public var data: [Dictionary<String, Any>]? 
    public var money:Decimal = 0.0 //This represents its current financial state 

    init(bizname: String,startbudget: Decimal){ 
     self.name = bizname 
     self.budget = startbudget 
     self.money = startbudget 
    } 
} 

PoolService123.swift

import Foundation 

class PoolService123 : Business{ 
    override init(bizname: String, startbudget: Decimal) { 
     //@JA - Call super to do generic business stuff that is the same 
     super.init(bizname: bizname, startbudget: startbudget) 

     self.data = [ 
      [ 
       "name":"Starting Budget", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Regions Targeting", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Marketing & Customers", 
       "columninfo" :["-"], 
       "highlightrow":["red":0.0,"green":0.0,"blue":0.0,"alpha":1.0,"tred":1.0,"tgreen":1.0,"tblue":1.0,"talpha":1.0], 
       "mark":true 
      ], 
      [ 
       "name":"Adwords Clicks", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Adwords Conversions", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Adwords Customers Acquired", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Customers Gained", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Customers Lost", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Total Customers", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Expenses", 
       "columninfo" :["-"], 
       "highlightrow":["red":0.2,"green":0.0,"blue":0.0,"alpha":1.0,"tred":1.0,"tgreen":1.0,"tblue":1.0,"talpha":1.0], 
       "mark":true 
      ], 
      [ 
       "name":"Adwords Cost", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Total Expenses", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Revenue", 
       "columninfo" :["-"], 
       "highlightrow":["red":0.0,"green":0.2,"blue":0.0,"alpha":1.0,"tred":1.0,"tgreen":1.0,"tblue":1.0,"talpha":1.0], 
       "mark":true 
      ], 
      [ 
       "name":"Gross Income", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Net Income", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Net Worth", 
       "columninfo" :["0"] 
      ], 
      [ 
       "name":"Business Partner Income", 
       "columninfo" :["0"] 
      ] 
     ] 
    } 
} 

事情我已经尝试:

当我重新模拟,并保持它在几个月它似乎是可以的,一切正常,这似乎只发生在我切换到多年,这使整体更少的列。

此外,如果我继续点击模拟,而在数年后它变得越来越糟。看起来像这最终... enter image description here

如果我每年开始,并继续做模拟,它工作正常。切换到每月,它工作正常!这样做后,切换回年,然后......它再次混乱。

很明显,当我从年度仿真到每月仿真时,我正在做的事情正在引发一个问题。

我注意到的其他东西是我做测试时错误的颜色和文本数据始终处于相同的位置。我在每个点上都在数据字典上做了一个断点,并且没有看到任何腐败现象。

一些有趣的结果调试

所以我加入这一行到这个功能func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

print("currentColumnIndex=\(currentColumnIndex ?? -1) , row=\(row)") 

我碰到下面的调试输出,当我做这年是很奇怪的,莫名其妙的按顺序渲染列然后再按顺序?

columnType=Yearly 
currentColumnIndex=0 , row=0 
currentColumnIndex=0 , row=1 
currentColumnIndex=0 , row=2 
currentColumnIndex=0 , row=3 
currentColumnIndex=0 , row=4 
currentColumnIndex=0 , row=5 
currentColumnIndex=0 , row=6 
currentColumnIndex=0 , row=7 
currentColumnIndex=0 , row=8 
currentColumnIndex=0 , row=9 
currentColumnIndex=0 , row=10 
currentColumnIndex=0 , row=11 
currentColumnIndex=0 , row=12 
currentColumnIndex=0 , row=13 
currentColumnIndex=0 , row=14 
currentColumnIndex=0 , row=15 
currentColumnIndex=0 , row=16 
currentColumnIndex=1 , row=0 
currentColumnIndex=1 , row=1 
currentColumnIndex=1 , row=2 
currentColumnIndex=1 , row=3 
currentColumnIndex=1 , row=4 
currentColumnIndex=1 , row=5 
currentColumnIndex=1 , row=6 
currentColumnIndex=1 , row=7 
currentColumnIndex=1 , row=8 
currentColumnIndex=1 , row=9 
currentColumnIndex=1 , row=10 
currentColumnIndex=1 , row=11 
currentColumnIndex=1 , row=12 
currentColumnIndex=1 , row=13 
currentColumnIndex=1 , row=14 
currentColumnIndex=1 , row=15 
currentColumnIndex=1 , row=16 
currentColumnIndex=2 , row=0 
currentColumnIndex=2 , row=1 
currentColumnIndex=2 , row=2 
currentColumnIndex=2 , row=3 
currentColumnIndex=2 , row=4 
currentColumnIndex=2 , row=5 
currentColumnIndex=2 , row=6 
currentColumnIndex=2 , row=7 
currentColumnIndex=2 , row=8 
currentColumnIndex=2 , row=9 
currentColumnIndex=2 , row=10 
currentColumnIndex=2 , row=11 
currentColumnIndex=2 , row=12 
currentColumnIndex=2 , row=13 
currentColumnIndex=2 , row=14 
currentColumnIndex=2 , row=15 
currentColumnIndex=2 , row=16 
currentColumnIndex=3 , row=0 
currentColumnIndex=3 , row=1 
currentColumnIndex=3 , row=2 
currentColumnIndex=3 , row=3 
currentColumnIndex=3 , row=4 
currentColumnIndex=3 , row=5 
currentColumnIndex=3 , row=6 
currentColumnIndex=3 , row=7 
currentColumnIndex=3 , row=8 
currentColumnIndex=3 , row=9 
currentColumnIndex=3 , row=10 
currentColumnIndex=3 , row=11 
currentColumnIndex=3 , row=12 
currentColumnIndex=3 , row=13 
currentColumnIndex=3 , row=14 
currentColumnIndex=3 , row=15 
currentColumnIndex=3 , row=16 
currentColumnIndex=4 , row=0 
currentColumnIndex=4 , row=1 
currentColumnIndex=4 , row=2 
currentColumnIndex=4 , row=3 
currentColumnIndex=4 , row=4 
currentColumnIndex=4 , row=5 
currentColumnIndex=4 , row=6 
currentColumnIndex=4 , row=7 
currentColumnIndex=4 , row=8 
currentColumnIndex=4 , row=9 
currentColumnIndex=4 , row=10 
currentColumnIndex=4 , row=11 
currentColumnIndex=4 , row=12 
currentColumnIndex=4 , row=13 
currentColumnIndex=4 , row=14 
currentColumnIndex=4 , row=15 
currentColumnIndex=4 , row=16 
currentColumnIndex=5 , row=0 
currentColumnIndex=5 , row=1 
currentColumnIndex=5 , row=2 
currentColumnIndex=5 , row=3 
currentColumnIndex=5 , row=4 
currentColumnIndex=5 , row=5 
currentColumnIndex=5 , row=6 
currentColumnIndex=5 , row=7 
currentColumnIndex=5 , row=8 
currentColumnIndex=5 , row=9 
currentColumnIndex=5 , row=10 
currentColumnIndex=5 , row=11 
currentColumnIndex=5 , row=12 
currentColumnIndex=5 , row=13 
currentColumnIndex=5 , row=14 
currentColumnIndex=5 , row=15 
currentColumnIndex=5 , row=16 
currentColumnIndex=6 , row=0 
currentColumnIndex=6 , row=1 
currentColumnIndex=6 , row=2 
currentColumnIndex=6 , row=3 
currentColumnIndex=6 , row=4 
currentColumnIndex=6 , row=5 
currentColumnIndex=6 , row=6 
currentColumnIndex=6 , row=7 
currentColumnIndex=6 , row=8 
currentColumnIndex=6 , row=9 
currentColumnIndex=6 , row=10 
currentColumnIndex=6 , row=11 
currentColumnIndex=6 , row=12 
currentColumnIndex=6 , row=13 
currentColumnIndex=6 , row=14 
currentColumnIndex=6 , row=15 
currentColumnIndex=6 , row=16 
currentColumnIndex=7 , row=0 
currentColumnIndex=7 , row=1 
currentColumnIndex=7 , row=2 
currentColumnIndex=7 , row=3 
currentColumnIndex=7 , row=4 
currentColumnIndex=7 , row=5 
currentColumnIndex=7 , row=6 
currentColumnIndex=7 , row=7 
currentColumnIndex=7 , row=8 
currentColumnIndex=7 , row=9 
currentColumnIndex=7 , row=10 
currentColumnIndex=7 , row=11 
currentColumnIndex=7 , row=12 
currentColumnIndex=7 , row=13 
currentColumnIndex=7 , row=14 
currentColumnIndex=7 , row=15 
currentColumnIndex=7 , row=16 
currentColumnIndex=8 , row=0 
currentColumnIndex=8 , row=1 
currentColumnIndex=8 , row=2 
currentColumnIndex=8 , row=3 
currentColumnIndex=8 , row=4 
currentColumnIndex=8 , row=5 
currentColumnIndex=8 , row=6 
currentColumnIndex=8 , row=7 
currentColumnIndex=8 , row=8 
currentColumnIndex=8 , row=9 
currentColumnIndex=8 , row=10 
currentColumnIndex=8 , row=11 
currentColumnIndex=8 , row=12 
currentColumnIndex=8 , row=13 
currentColumnIndex=8 , row=14 
currentColumnIndex=8 , row=15 
currentColumnIndex=8 , row=16 
currentColumnIndex=9 , row=0 
currentColumnIndex=9 , row=1 
currentColumnIndex=9 , row=2 
currentColumnIndex=9 , row=3 
currentColumnIndex=9 , row=4 
currentColumnIndex=9 , row=5 
currentColumnIndex=9 , row=6 
currentColumnIndex=9 , row=7 
currentColumnIndex=9 , row=8 
currentColumnIndex=9 , row=9 
currentColumnIndex=9 , row=10 
currentColumnIndex=9 , row=11 
currentColumnIndex=9 , row=12 
currentColumnIndex=9 , row=13 
currentColumnIndex=9 , row=14 
currentColumnIndex=9 , row=15 
currentColumnIndex=9 , row=16 
currentColumnIndex=10 , row=0 
currentColumnIndex=10 , row=1 
currentColumnIndex=10 , row=2 
currentColumnIndex=10 , row=3 
currentColumnIndex=10 , row=4 
currentColumnIndex=10 , row=5 
currentColumnIndex=10 , row=6 
currentColumnIndex=10 , row=7 
currentColumnIndex=10 , row=8 
currentColumnIndex=10 , row=9 
currentColumnIndex=10 , row=10 
currentColumnIndex=10 , row=11 
currentColumnIndex=10 , row=12 
currentColumnIndex=10 , row=13 
currentColumnIndex=10 , row=14 
currentColumnIndex=10 , row=15 
currentColumnIndex=10 , row=16 
currentColumnIndex=0 , row=0 
currentColumnIndex=1 , row=0 
currentColumnIndex=2 , row=0 
currentColumnIndex=3 , row=0 
currentColumnIndex=4 , row=0 
currentColumnIndex=5 , row=0 
currentColumnIndex=6 , row=0 
currentColumnIndex=7 , row=0 
currentColumnIndex=8 , row=0 
currentColumnIndex=9 , row=0 
currentColumnIndex=10 , row=0 
currentColumnIndex=0 , row=1 
currentColumnIndex=1 , row=1 
currentColumnIndex=2 , row=1 
currentColumnIndex=3 , row=1 
currentColumnIndex=4 , row=1 
currentColumnIndex=5 , row=1 
currentColumnIndex=6 , row=1 
currentColumnIndex=7 , row=1 
currentColumnIndex=8 , row=1 
currentColumnIndex=9 , row=1 
currentColumnIndex=10 , row=1 
currentColumnIndex=0 , row=2 
currentColumnIndex=1 , row=2 
currentColumnIndex=2 , row=2 
currentColumnIndex=3 , row=2 
currentColumnIndex=4 , row=2 
currentColumnIndex=5 , row=2 
currentColumnIndex=6 , row=2 
currentColumnIndex=7 , row=2 
currentColumnIndex=8 , row=2 
currentColumnIndex=9 , row=2 
currentColumnIndex=10 , row=2 
currentColumnIndex=0 , row=3 
currentColumnIndex=1 , row=3 
currentColumnIndex=2 , row=3 
currentColumnIndex=3 , row=3 
currentColumnIndex=4 , row=3 
currentColumnIndex=5 , row=3 
currentColumnIndex=6 , row=3 
currentColumnIndex=7 , row=3 
currentColumnIndex=8 , row=3 
currentColumnIndex=9 , row=3 
currentColumnIndex=10 , row=3 
currentColumnIndex=0 , row=4 
currentColumnIndex=1 , row=4 
currentColumnIndex=2 , row=4 
currentColumnIndex=3 , row=4 
currentColumnIndex=4 , row=4 
currentColumnIndex=5 , row=4 
currentColumnIndex=6 , row=4 
currentColumnIndex=7 , row=4 
currentColumnIndex=8 , row=4 
currentColumnIndex=9 , row=4 
currentColumnIndex=10 , row=4 
currentColumnIndex=0 , row=5 
currentColumnIndex=1 , row=5 
currentColumnIndex=2 , row=5 
currentColumnIndex=3 , row=5 
currentColumnIndex=4 , row=5 
currentColumnIndex=5 , row=5 
currentColumnIndex=6 , row=5 
currentColumnIndex=7 , row=5 
currentColumnIndex=8 , row=5 
currentColumnIndex=9 , row=5 
currentColumnIndex=10 , row=5 
currentColumnIndex=0 , row=6 
currentColumnIndex=1 , row=6 
currentColumnIndex=2 , row=6 
currentColumnIndex=3 , row=6 
currentColumnIndex=4 , row=6 
currentColumnIndex=5 , row=6 
currentColumnIndex=6 , row=6 
currentColumnIndex=7 , row=6 
currentColumnIndex=8 , row=6 
currentColumnIndex=9 , row=6 
currentColumnIndex=10 , row=6 
currentColumnIndex=0 , row=7 
currentColumnIndex=1 , row=7 
currentColumnIndex=2 , row=7 
currentColumnIndex=3 , row=7 
currentColumnIndex=4 , row=7 
currentColumnIndex=5 , row=7 
currentColumnIndex=6 , row=7 
currentColumnIndex=7 , row=7 
currentColumnIndex=8 , row=7 
currentColumnIndex=9 , row=7 
currentColumnIndex=10 , row=7 
currentColumnIndex=0 , row=8 
currentColumnIndex=1 , row=8 
currentColumnIndex=2 , row=8 
currentColumnIndex=3 , row=8 
currentColumnIndex=4 , row=8 
currentColumnIndex=5 , row=8 
currentColumnIndex=6 , row=8 
currentColumnIndex=7 , row=8 
currentColumnIndex=8 , row=8 
currentColumnIndex=9 , row=8 
currentColumnIndex=10 , row=8 
currentColumnIndex=0 , row=9 
currentColumnIndex=1 , row=9 
currentColumnIndex=2 , row=9 
currentColumnIndex=3 , row=9 
currentColumnIndex=4 , row=9 
currentColumnIndex=5 , row=9 
currentColumnIndex=6 , row=9 
currentColumnIndex=7 , row=9 
currentColumnIndex=8 , row=9 
currentColumnIndex=9 , row=9 
currentColumnIndex=10 , row=9 
currentColumnIndex=0 , row=10 
currentColumnIndex=1 , row=10 
currentColumnIndex=2 , row=10 
currentColumnIndex=3 , row=10 
currentColumnIndex=4 , row=10 
currentColumnIndex=5 , row=10 
currentColumnIndex=6 , row=10 
currentColumnIndex=7 , row=10 
currentColumnIndex=8 , row=10 
currentColumnIndex=9 , row=10 
currentColumnIndex=10 , row=10 
currentColumnIndex=0 , row=11 
currentColumnIndex=1 , row=11 
currentColumnIndex=2 , row=11 
currentColumnIndex=3 , row=11 
currentColumnIndex=4 , row=11 
currentColumnIndex=5 , row=11 
currentColumnIndex=6 , row=11 
currentColumnIndex=7 , row=11 
currentColumnIndex=8 , row=11 
currentColumnIndex=9 , row=11 
currentColumnIndex=10 , row=11 
currentColumnIndex=0 , row=12 
currentColumnIndex=1 , row=12 
currentColumnIndex=2 , row=12 
currentColumnIndex=3 , row=12 
currentColumnIndex=4 , row=12 
currentColumnIndex=5 , row=12 
currentColumnIndex=6 , row=12 
currentColumnIndex=7 , row=12 
currentColumnIndex=8 , row=12 
currentColumnIndex=9 , row=12 
currentColumnIndex=10 , row=12 
currentColumnIndex=0 , row=13 
currentColumnIndex=1 , row=13 
currentColumnIndex=2 , row=13 
currentColumnIndex=3 , row=13 
currentColumnIndex=4 , row=13 
currentColumnIndex=5 , row=13 
currentColumnIndex=6 , row=13 
currentColumnIndex=7 , row=13 
currentColumnIndex=8 , row=13 
currentColumnIndex=9 , row=13 
currentColumnIndex=10 , row=13 
currentColumnIndex=0 , row=14 
currentColumnIndex=1 , row=14 
currentColumnIndex=2 , row=14 
currentColumnIndex=3 , row=14 
currentColumnIndex=4 , row=14 
currentColumnIndex=5 , row=14 
currentColumnIndex=6 , row=14 
currentColumnIndex=7 , row=14 
currentColumnIndex=8 , row=14 
currentColumnIndex=9 , row=14 
currentColumnIndex=10 , row=14 
currentColumnIndex=0 , row=15 
currentColumnIndex=1 , row=15 
currentColumnIndex=2 , row=15 
currentColumnIndex=3 , row=15 
currentColumnIndex=4 , row=15 
currentColumnIndex=5 , row=15 
currentColumnIndex=6 , row=15 
currentColumnIndex=7 , row=15 
currentColumnIndex=8 , row=15 
currentColumnIndex=9 , row=15 
currentColumnIndex=10 , row=15 
currentColumnIndex=0 , row=16 
currentColumnIndex=1 , row=16 
currentColumnIndex=2 , row=16 
currentColumnIndex=3 , row=16 
currentColumnIndex=4 , row=16 
currentColumnIndex=5 , row=16 
currentColumnIndex=6 , row=16 
currentColumnIndex=7 , row=16 
currentColumnIndex=8 , row=16 
currentColumnIndex=9 , row=16 
currentColumnIndex=10 , row=16 

这个结果有趣的是当每月呈现默认呈现时我这样做似乎是以不同的顺序呈现tableView?

columnType=Monthly 
currentColumnIndex=0 , row=0 
currentColumnIndex=1 , row=0 
currentColumnIndex=2 , row=0 
currentColumnIndex=3 , row=0 
currentColumnIndex=4 , row=0 
currentColumnIndex=5 , row=0 
currentColumnIndex=6 , row=0 
currentColumnIndex=7 , row=0 
currentColumnIndex=8 , row=0 
currentColumnIndex=9 , row=0 
currentColumnIndex=10 , row=0 
currentColumnIndex=11 , row=0 
currentColumnIndex=12 , row=0 
... 

currentColumnIndex=0 , row=0 
currentColumnIndex=1 , row=0 
currentColumnIndex=2 , row=0 
currentColumnIndex=3 , row=0 
currentColumnIndex=4 , row=0 
currentColumnIndex=5 , row=0 
currentColumnIndex=6 , row=0 
currentColumnIndex=7 , row=0 
currentColumnIndex=8 , row=0 
currentColumnIndex=9 , row=0 
currentColumnIndex=10 , row=0 
currentColumnIndex=11 , row=0 
currentColumnIndex=12 , row=0 
... 
currentColumnIndex=0 , row=1 
currentColumnIndex=1 , row=1 
currentColumnIndex=2 , row=1 
currentColumnIndex=3 , row=1 
currentColumnIndex=4 , row=1 
currentColumnIndex=5 , row=1 
currentColumnIndex=6 , row=1 
currentColumnIndex=7 , row=1 
currentColumnIndex=8 , row=1 
currentColumnIndex=9 , row=1 
currentColumnIndex=10 , row=1 
currentColumnIndex=11 , row=1 
currentColumnIndex=12 , row=1 
.... 
currentColumnIndex=0 , row=2 
currentColumnIndex=1 , row=2 
currentColumnIndex=2 , row=2 
currentColumnIndex=3 , row=2 
currentColumnIndex=4 , row=2 
currentColumnIndex=5 , row=2 
currentColumnIndex=6 , row=2 
currentColumnIndex=7 , row=2 
currentColumnIndex=8 , row=2 
currentColumnIndex=9 , row=2 
currentColumnIndex=10 , row=2 
currentColumnIndex=11 , row=2 
currentColumnIndex=12 , row=2 

... currentColumnIndex = 120,排= 16

我没有足够的字符显示全模式,但在这里总结它。

清除表的结果似乎只是暂时帮助,我通过设置data = []并重新加载表来完成。

回答

3

首先它不是内存问题。

表视图单元格被重用,你必须确保所有的UI元素都被设置为一个定义的状态。

的代码包含一个if条款,所以你必须添加一个平衡else子句来设置颜色为默认值,例如:

if let bghighlight = data[row]["highlightrow"] as? [String:NSNumber] { 
     cell.textField?.backgroundColor = NSColor(red: CGFloat(bghighlight["red"]!), green: CGFloat(bghighlight["green"]!), blue: CGFloat(bghighlight["blue"]!), alpha: CGFloat(bghighlight["alpha"]!)) 
     cell.textField?.textColor = NSColor(red: CGFloat(bghighlight["tred"]!), green: CGFloat(bghighlight["tgreen"]!), blue: CGFloat(bghighlight["tblue"]!), alpha: CGFloat(bghighlight["talpha"]!)) 
    } else { 
     cell.textField?.backgroundColor = .white 
     cell.textField?.textColor = .textColor 
    } 

我改变了代码一点使用可选的绑定,这更多高效。 PS:我建议将JSON映射到数据模型,一次又一次创建相同的颜色是不必要的昂贵。

+0

现在试试这一秒 –

+0

我不得不删除其他标记,但它似乎可以解决颜色问题。其他值为零的表格单元格由于数据仍然看起来很奇怪,但似乎已经修复了它。我会在这些情况下更新它的样子。谢谢你。 –

+0

您有任何示例如何将JSON映射到数据模型?我对MacOs桌子的工作方式还是有点新的 –