向 NSTableView 添加数据时崩溃
Crash by adding data to NSTableView
我正在尝试使用 NSArrayController
将使用 classes 构建的数组中的数据添加到 NSScrollView
这是 class 和数组:
class Serie: NSObject {
@objc dynamic var theID: String
@objc dynamic var serieName: String
@objc dynamic var serieAired: String
@objc dynamic var serieNetwork: String
@objc dynamic var serieStatus: String
init(theID: String, serieName: String, serieAired: String, serieNetwork: String, serieStatus: String ) {
self.theID = theID
self.serieName = serieName
self.serieAired = serieAired
self.serieNetwork = serieNetwork
self.serieStatus = serieStatus
}
}
@objc dynamic var Series: [Serie] = []
接下来,我将 NSArrayController 绑定到 classes "Series" 的数组,并将 TableViewCell 链接到每个 class 项。
TableView tree image
Bindings & Outlets of ArrayController image
Bindings & Outlets of TableViewCell image
下一个函数是我填充 "dynamic var Series" 的地方,它将内容绑定到 NSArrayController 并填充 NSScrollView:
func GetSerieID(theSerieName: String){
let url = URL(string: ("https://api.thetvdb.com/search/series?name=" + theSerieName ))!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue( "Bearer \(token)", forHTTPHeaderField: "Authorization")
//request.setValue( "en", forHTTPHeaderField: "Accept-Language")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let json = try? JSON(data: data) // >> I use SwiftyJSON to manage JSON
let theJSONContent = json!["data"]
self.Series = [] // >> Empty the array Series
for (_,subJson):(String, JSON) in theJSONContent {
let serie = Serie(
theID: subJson["id"].stringValue,
serieName: subJson["seriesName"].stringValue,
serieAired: subJson["firstAired"].stringValue,
serieNetwork: subJson["network"].stringValue,
serieStatus: subJson["status"].stringValue
)
self.Series.append(serie) // >> Here is where it crashes
}
}
task.resume()
serieTabla.reloadData()
}
有时,TableView 不会正确显示或更新视图并加载空白数据,如果您单击它会显示几秒钟,但通常它会工作,直到加载超过 10 个结果。 问题来了。
它是随机发生的,但通常当您超过 10-20 件时。
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
崩溃日志:
miVideoGestor[82269:13639248] *** Assertion failure in -[NSTableRowData _addRowViewForVisibleRow:withPriorView:]
我想不出解决办法。
有大神能教教我怎么解决tableView加载和显示数据的问题吗?
提前谢谢大家
当您使用基于键值观察的 Cocoa 绑定时,您必须将影响 UI 的任何行放入 DispatchQueue.main
块
DispatchQueue.main.async {
self.Series = []
for (_,subJson):(String, JSON) in theJSONContent {
let serie = Serie(
theID: subJson["id"].stringValue,
serieName: subJson["seriesName"].stringValue,
serieAired: subJson["firstAired"].stringValue,
serieNetwork: subJson["network"].stringValue,
serieStatus: subJson["status"].stringValue
)
self.Series.append(serie)
}
self.serieTabla.reloadData()
}
我正在尝试使用 NSArrayController
将使用 classes 构建的数组中的数据添加到 NSScrollView这是 class 和数组:
class Serie: NSObject {
@objc dynamic var theID: String
@objc dynamic var serieName: String
@objc dynamic var serieAired: String
@objc dynamic var serieNetwork: String
@objc dynamic var serieStatus: String
init(theID: String, serieName: String, serieAired: String, serieNetwork: String, serieStatus: String ) {
self.theID = theID
self.serieName = serieName
self.serieAired = serieAired
self.serieNetwork = serieNetwork
self.serieStatus = serieStatus
}
}
@objc dynamic var Series: [Serie] = []
接下来,我将 NSArrayController 绑定到 classes "Series" 的数组,并将 TableViewCell 链接到每个 class 项。
TableView tree image
Bindings & Outlets of ArrayController image
Bindings & Outlets of TableViewCell image
下一个函数是我填充 "dynamic var Series" 的地方,它将内容绑定到 NSArrayController 并填充 NSScrollView:
func GetSerieID(theSerieName: String){
let url = URL(string: ("https://api.thetvdb.com/search/series?name=" + theSerieName ))!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue( "Bearer \(token)", forHTTPHeaderField: "Authorization")
//request.setValue( "en", forHTTPHeaderField: "Accept-Language")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let json = try? JSON(data: data) // >> I use SwiftyJSON to manage JSON
let theJSONContent = json!["data"]
self.Series = [] // >> Empty the array Series
for (_,subJson):(String, JSON) in theJSONContent {
let serie = Serie(
theID: subJson["id"].stringValue,
serieName: subJson["seriesName"].stringValue,
serieAired: subJson["firstAired"].stringValue,
serieNetwork: subJson["network"].stringValue,
serieStatus: subJson["status"].stringValue
)
self.Series.append(serie) // >> Here is where it crashes
}
}
task.resume()
serieTabla.reloadData()
}
有时,TableView 不会正确显示或更新视图并加载空白数据,如果您单击它会显示几秒钟,但通常它会工作,直到加载超过 10 个结果。 问题来了。
它是随机发生的,但通常当您超过 10-20 件时。
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
崩溃日志:
miVideoGestor[82269:13639248] *** Assertion failure in -[NSTableRowData _addRowViewForVisibleRow:withPriorView:]
我想不出解决办法。
有大神能教教我怎么解决tableView加载和显示数据的问题吗?
提前谢谢大家
当您使用基于键值观察的 Cocoa 绑定时,您必须将影响 UI 的任何行放入 DispatchQueue.main
块
DispatchQueue.main.async {
self.Series = []
for (_,subJson):(String, JSON) in theJSONContent {
let serie = Serie(
theID: subJson["id"].stringValue,
serieName: subJson["seriesName"].stringValue,
serieAired: subJson["firstAired"].stringValue,
serieNetwork: subJson["network"].stringValue,
serieStatus: subJson["status"].stringValue
)
self.Series.append(serie)
}
self.serieTabla.reloadData()
}