前提
本搜索功能主要是在集合套字典的數(shù)據(jù)源中實(shí)現(xiàn)
實(shí)現(xiàn)步驟
1.提供謂詞搜索關(guān)鍵字
2.提供搜索區(qū)域
3.將搜索關(guān)鍵字和謂詞結(jié)合
4.接收搜索結(jié)果
數(shù)據(jù)源
數(shù)據(jù)源樣式為集合套字典
屏幕快照 2016-06-02 上午10.54.08.png
核心搜索代碼
(1)self.dataList查詢數(shù)據(jù)源
(2)arr 按照規(guī)定的key值進(jìn)行數(shù)據(jù)查詢
(3)searchStr 搜索關(guān)鍵字
(4)_searchResultArr 搜索結(jié)果
//搜索關(guān)鍵字
NSString *searchStr =self.titlename;
//按照規(guī)定的key值進(jìn)行數(shù)據(jù)查詢
NSArray *arr=@[@"MCType",@"MCCompanyname",@"MCIntroduce",@"MCPlace",@"MCTime",@"Money",@"MCRequirement"];
_predicateStr=[NSMutableString stringWithFormat:@""];
for (NSString *parameterStr in arr) {
[_predicateStr appendString:[NSString stringWithFormat:@"self.%@ CONTAINS [CD] '%@' OR ",parameterStr,searchStr]];
}
[_predicateStr deleteCharactersInRange:NSMakeRange(_predicateStr.length-4,4)];
NSPredicate *predicate = [NSPredicate predicateWithFormat:_predicateStr];
//查詢結(jié)果
//self.dataList查詢數(shù)據(jù)源
_searchResultArr = [NSMutableArray arrayWithArray:[self.dataList filteredArrayUsingPredicate:predicate]];
通過搜索結(jié)果后顯示在tabview上
Simulator Screen Shot 2016年6月2日 上午11.05.05.png
Simulator Screen Shot 2016年6月2日 上午11.05.50.png