Hi,
I use a predicate to filter a NSTableView when I type in a NSSearchField :
private func _filterTracks(str: String) {
var thePredicates = [NSPredicate]()
let theArtistPredicate = NSPredicate(format: FilterType.format, FilterType.artistField, str)
let theTitlePredicate = NSPredicate(format: FilterType.format, FilterType.titleField, str)
let theAlbumPredicate = NSPredicate(format: FilterType.format, FilterType.albumField, str)
switch _filter?.tag {
case FilterType.FilterListType.ALL.rawValue:
thePredicates.append(theArtistPredicate)
thePredicates.append(theTitlePredicate)
thePredicates.append(theAlbumPredicate)
case FilterType.FilterListType.artist.rawValue:
thePredicates.append(theArtistPredicate)
case FilterType.FilterListType.album.rawValue:
thePredicates.append(theAlbumPredicate)
case FilterType.FilterListType.title.rawValue:
thePredicates.append(theTitlePredicate)
default:
thePredicates.append(theArtistPredicate)
thePredicates.append(theTitlePredicate)
thePredicates.append(theAlbumPredicate)
}
let theCompoundPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: thePredicates)
arrayController.filterPredicate = theCompoundPredicate
}
But when the NSSearchField is reset, how to reset the filter and get the datas in the NSTableView like it was ?
Thx.
Yes, searchFieldDidEndSearching is called each time the NSSearchField is cleared. It is working.
And _filterTracks is called each time a char is typed in the NSSearchField in the controlTextDidChange method :
func controlTextDidChange(_ obj: Notification) {
let theTextField = obj.object as! NSTextField
let theStr = theTextField.stringValue
_filterTracks(str: theStr)
}
So each time a char is typed, the predicate is set to the NSArrayController.
And after that, I "just" need to find how to clear the predicate once the NSSearchField is cleared, like we have in iTunes / Music app...
Thx.