After a while being stucked with this problem, I finally found the solution in 5 minutes :
When a NSSearchField is cleared, 2 functions are called : controlTextDidChange and searchFieldDidEndSearching.
So I just add a control to check if the filter string count is equal to 0 to reset the filter :
private func _filterTracks(str: String) {
if str.count == 0 {
resetFilter()
return
}
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)
if(_currentFilter?.tag == FilterType.FilterListType.ALL.rawValue){
thePredicates.append(theArtistPredicate)
thePredicates.append(theTitlePredicate)
thePredicates.append(theAlbumPredicate)
}else if(_currentFilter?.tag == FilterType.FilterListType.artist.rawValue){
thePredicates.append(theArtistPredicate)
}else if(_currentFilter?.tag == FilterType.FilterListType.album.rawValue){
thePredicates.append(theAlbumPredicate)
}else if(_currentFilter?.tag == FilterType.FilterListType.title.rawValue){
thePredicates.append(theTitlePredicate)
}
let theCompoundPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: thePredicates)
arrayController.filterPredicate = theCompoundPredicate
print("V&G_Project____filterTracks : ", arrayController.filterPredicate?.predicateFormat)
}
func resetFilter() {
searchField.stringValue = "" // in case the func is called from another func
arrayController.filterPredicate = nil
}
Et voila...
So that's right it is arrayController.filterPredicate = nil to reset the predicate.
Thx.
Topic:
Programming Languages
SubTopic:
Swift
Tags: