Cocoa swift-在tableview上缓慢加载数据
我有一个将歌曲信息加载到tableview的功能。如果我选择包含20首以上歌曲的播放列表,速度会非常慢Cocoa swift-在tableview上缓慢加载数据,swift,cocoa,Swift,Cocoa,我有一个将歌曲信息加载到tableview的功能。如果我选择包含20首以上歌曲的播放列表,速度会非常慢 func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { if tableColumn?.identifier == "TitleCol" { if let cell = tableView.make(withIdenti
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if tableColumn?.identifier == "TitleCol" {
if let cell = tableView.make(withIdentifier: "TitleCell", owner: self) as? NSTableCellView {
do{
let library = try ITLibrary(apiVersion: "1.1")
let thePlaylist = library.allPlaylists
DispatchQueue.main.async {
cell.textField?.stringValue = thePlaylist[self.playlist_index - 1].items[row].title
}
return cell
}
catch let error as NSError {
print(error)
}
}
} else {
if let cell = tableView.make(withIdentifier: "NumberCell", owner: self) as? NSTableCellView {
cell.textField?.stringValue = String(row + 1)
return cell
}
}
return nil
}
但问题是,当我开始滚动时,tableView非常滞后 在
viewForColumn:Row:
的每次调用中获取(相同的)数据源非常昂贵。将行let library=try ITLibrary(apiVersion:“1.1”)
和let thePlaylist=library.allPlaylists
移动到只调用一次的位置。我还建议使用Cocoa绑定,这会使整个数据源方法viewForColumn:Row:
过时。哇,这是一个多么简单的想法,谢谢你。它起作用了