您好,登錄后才能下訂單哦!
在ios開發(fā)中搜索條的使用挺常見的,不過之前一直沒用到也沒細(xì)細(xì)研究,最近做外包項目的時候剛好用到,在這里記錄一下使用的過程,只要理解了原理,其實還是比較簡單的!上傳的圖片有點大,剛好可以看清楚它的使用效果!
我喜歡一步步的解析控件使用過程,其實真正的用心做一件事情是很享受的,雖然現(xiàn)在的社會都很注重效率和回報,這也是中國這種社會環(huán)境下難出大師級的人物的很重要的一個因素。扯得有點遠(yuǎn)了,只是希望國內(nèi)開發(fā)者不要太急功近利,熟練應(yīng)用本不是難事,一點點積累就好。下面開始搜索條
UISearchBar 和 UISearchDisplayController 的使用介紹。
第一步:添加UISearchBarDelegate,UISearchDisplayDelegate 委托
第二步:就是UISearchBar 和UISearchDisplayController的使用
//搜索框
self.searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(10, 15, self.view.bounds.size.width-20, 45)]autorelease];
searchBar.placeholder=@"輸入城市名或拼音";
searchBar.delegate = self;
searchBar.backgroundColor=[UIColor clearColor];
searchBar.backgroundImage=[UIImage p_w_picpathNamed:@"搜索背景"];
searchBar.keyboardType=UIKeyboardTypeDefault;
myTableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchdispalyCtrl = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]autorelease];
//設(shè)置取消按鈕文字顏色
searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;
//search dispalyCtrl.active = NO;
//[searchdispalyCtrl.searchResultsTableView setHidden:NO];
searchdispalyCtrl.delegate = self;
searchdispalyCtrl.searchResultsDelegate=self;
searchdispalyCtrl.searchResultsDataSource = self;
這里面有兩個點需要注意一下,一個是 myTableView.tableHeaderView = searchBar;這里是將searchbar放在對應(yīng)列表的第一個section之中,而且section的高度不用設(shè)置,當(dāng)然可以通過addsubview添加這種方式會產(chǎn)生一種移位,建議默認(rèn)的方式比較好些!另一點是UISearchDisplayController是對searchbar的進一步封裝,里面的東西確實簡化了好多,可以采用這種方法設(shè)置取消按鈕文本的顏色searchdispalyCtrl.searchBar.tintColor=BB_Yanzheng_Color;
第三步:相關(guān)委托的使用
我們通過實驗發(fā)現(xiàn)點擊searchbar的輸入框會彈出一個獨立的tableview視圖,并顯示一個取消按鈕,搜索的數(shù)據(jù)顯示在這個獨立的視圖列表中。而這個視圖列表和普通的列表用法一樣,需要設(shè)置
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return 26;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"我去:%@",searchResult);
return [searchResult count];
}
else
{
if (section==0||section==1||section==2||section==3)
{
return 1;
}
if (isZiMu)
{
NSLog(@"zimugeshu:%@,%d",[zimuList objectAtIndex:section-4],[[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count]);
return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];
}
/*
char mychar='a';
//當(dāng)前字母
NSString *zimu=[NSString stringWithFormat:@"%c",mychar-32];
//NSString *zimu=[NSString stringWithFormat:@"%c",c];
if (![zimu isEqualToString:@"i"]&&![zimu isEqualToString:@"o"]&&![zimu isEqualToString:@"u"]&&![zimu isEqualToString:@"v"])
{
NSLog(@"zimugeshu:%@,%d",zimu,[[cityGroupList objectForKey:zimu]count]);
return [[cityGroupList objectForKey:[zimuList objectAtIndex:section-4]]count];
}*/
return 0;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 45;
}
if (indexPath.section==0)
{
return 0;
}
if (indexPath.section==1||indexPath.section==2)
{
return 73;
}
if (indexPath.section==3)
{
//超過的話固定顯示203
if (17+([remenCityList count]/3+1)*47+18<=223)
{
if ([remenCityList count]!=0&&[remenCityList count]%3==0)
{
return 17+([remenCityList count]/3)*47;
}
return 17+([remenCityList count]/3+1)*47;
}
return 223;
}
return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 0;
}
if (section!=0)
{
return 32;
}
return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 0.01;
}
return 0.01f;
}
這些個基本的高度和行數(shù) 模塊信息,當(dāng)然列表的顯示更是必不可少
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
}
else
{
for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperview];
}
}
//cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
//[cell.textLabel setTextColor:BB_Red_Color];
//NSLog(@"zheli:%@,%d,%@",[searchResult objectAtIndex:indexPath.row],indexPath.row,self.searchDisplayController.searchResultsTableView);
//城市名稱
UILabel *labLeft = [[[UILabel alloc]init]autorelease];
labLeft.backgroundColor = [UIColor clearColor];
labLeft.textAlignment = NSTextAlignmentLeft;
labLeft.font = [UIFont systemFontOfSize:15];
labLeft.textColor = [UIColor blackColor];
labLeft.frame = CGRectMake(15, 15, 200, 15);
labLeft.text=[searchResult objectAtIndex:indexPath.row];
[cell.contentView addSubview:labLeft];
return cell;
}
}
上面列出的只是顯示信息,真正數(shù)據(jù)的搜索要依賴于下面的委托方法實現(xiàn)
#pragma mark - UISearchDisplayDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];
NSLog(@"woshuoba:%@,%@,%d,%@",searchString,self.searchBar.scopeButtonTitles,self.searchBar.selectedScopeButtonIndex,[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]);
return YES;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSMutableArray *tempResults = [[[NSMutableArray array]init]autorelease];
NSMutableArray *tempResultsId = [[[NSMutableArray array]init]autorelease];
[tempResults removeAllObjects];
[tempResultsId removeAllObjects];
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSLog(@"sousuo:%@,%@,%@",searchText,scope,storeResults);
for (int i = 0; i < storeResults.count; i++)
{
NSString *storeString = [[storeResults objectAtIndex:i]objectForKey:@"name"];
NSRange storeRange = NSMakeRange(0, storeString.length);
NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];
if (foundRange.length)
{
NSLog(@"foundRange:%d,%d",foundRange.length,foundRange.location);
[tempResults addObject:storeString];
[tempResultsId addObject:[[storeResults objectAtIndex:i]objectForKey:@"id"]];
}
NSLog(@"wodejieguo:%@,%@",[[storeResults objectAtIndex:i]objectForKey:@"id"],storeString);
}
//清空搜索內(nèi)容Id
[self.searchResultId removeAllObjects];
[self.searchResult removeAllObjects];
[self.searchResultId addObjectsFromArray:tempResultsId];
[self.searchResult addObjectsFromArray:tempResults];
NSLog(@"searchResult:%@,%@",searchResult,searchResultId);
}
說道這里關(guān)于搜索條的使用已經(jīng)基本完成,當(dāng)然搜索列表選中跳頁可以借助自定義委托來實現(xiàn),這里就不多少了!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。