SWIFT 3 OSX可可阵列控制器和CoreData绑定“在多列上搜索”

时间:2016-11-11 02:25:50

标签: swift cocoa nstableview nsarraycontroller nssearchfield

我有一个coredata实体绑定到一个包含5列的tableView。 我正在使用数组控制器来管理tableview排序等。

我的问题是使用绑定进行搜索。我有一个绑定到ArrayController的searchField,谓词格式设置为第一列数据。一切正常。我还可以在XCODE中更改谓词格式并搜索不同的列。

我遇到的问题是知道如何更改我的代码中的谓词格式,以允许用户搜索他们选择的列...

我尝试按如下方式修改ArrayController:

let predicate = NSPredicate(format: "projectNumber contains[cd] %@", searchBar.stringValue)
    searchArrayController.filterPredicate = predicate

let predicate = NSPredicate(format: "customerName contains[cd] %@", searchBar.stringValue)
    searchArrayController.filterPredicate = predicate

projectNumber和customerName是我的实体的属性。

当我以这种方式设置谓词时,表数据消失了。我可以通过摆弄搜索字段再次显示它,但它忽略了我的谓词设置,只服从XCODE故事板中的原始设置。

StoryBoard设置是: 谓词格式= projectNumber包含[cd] $ value

提前感谢您提供任何帮助......

1 个答案:

答案 0 :(得分:0)

为每列添加谓词绑定。绑定搜索字段的def index(request): clothes = Clothes_Item.objects.all() filter_category = request.GET.get('filter_category') if filter_category: clothes = clothes.filter(gender=filter_category) if request.user.is_authenticated(): favorite_clothes_ids = get_favorite_clothes_ids(request) return render(request, 'index.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids}) return render(request, 'index.html', {'clothes': clothes}) 时,会显示绑定predicate。无需编码。