#import "MainViewController.h"
@implementation MainViewController
{
//数据源数组
NSMutableArray*_dataArray;
//搜索结果数组
NSMutableArray*_resultArray;
UITableView*_tableView;
UISearchBar*_searchBar;
}
- (void)dealloc {
[_searchBar release];
[_tableView release];
[_dataArray release];
[_resultArray release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//实例化数组
_resultArray=[[NSMutableArray alloc]init];
_dataArray=[[NSMutableArray alloc]init];
//坐标设置要适合,不然最后的部分有可能显示不出来
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,320,416) style:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
[self.view addSubview:_tableView];
//组织数据源
for(int i=97;i<123;i++)
{
NSMutableArray*subArray=[[NSMutableArray alloc]init];
for(int j=0;j<10;j++)
{
[subArray addObject:[NSString stringWithFormat:@"%c%d",i,j]];
}
[_dataArray addObject:subArray];
}
//搜索条
_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
//把搜索条加到headerView上,其实不是很好,可以加到view上更好
_tableView.tableHeaderView=_searchBar;
[_searchBar release];
//搜索控制器
UISearchDisplayController*sdc=[[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
//设置代理(不需要遵循协议)
sdc.searchResultsDataSource=self;
sdc.searchResultsDelegate=self;
}
//搜索结果界面tableView是系统自动给添加的,不是之前自己写的—_tableView.下面的设置分两部分,一部分是设置_tableView的显示,另一部分是设置搜索结果界面的显示
//设置section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView!=_tableView)
return 1;
return [_dataArray count];
}
//设置rows的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView!=_tableView)
{
//先清空数组里的内容。每次搜索显示的不能一样吧。
[_resultArray removeAllObjects];
//把输入的内容与原有数据源比较,有相似的就加到_resultArray数组里
for(NSMutableArray*mArray in _dataArray)
{
for(NSString*str in mArray)
{
NSRange range=[str rangeOfString:_searchBar.text];
if(range.location!=NSNotFound){
[_resultArray addObject:str];
}
}
}
return [_resultArray count];
}
else
return [[_dataArray objectAtIndex:section]count];
}
//设置section的title
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView!=_tableView){
return @"搜索结果";
}
else
return [NSString stringWithFormat:@"%c",97+section];
}
//Cell的相关设置
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellName=@"cell";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellName];
if(cell==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName]autorelease];
}
if(tableView!=_tableView){
cell.textLabel.text=[_resultArray objectAtIndex:indexPath.row];
}else{
cell.textLabel.text=[[_dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
}
return cell;
}
//设置索引条
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray*titles=[[NSMutableArray alloc]init];
//加上了放大镜,如果搜索框加到view上,让其不动的话,不用加
[titles addObject:UITableViewIndexSearch];
//加入其他内容
for(int i=97;i<123;i++)
{
[titles addObject:[NSString stringWithFormat:@"%c",i]];
}
return [titles autorelease];
}
//设置索引条对应关系
//如果不加放大镜。这个方法就不需要。
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if(index==0)
{
//index是索引条的序号。从0开始,所以第0 个是放大镜。如果是放大镜坐标就移动到搜索框处
[tableView scrollRectToVisible:_searchBar.frame animated:NO];
return -1;
}else{
//因为返回的值是section的值。所以减1就是与section对应的值了
return index-1;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
iOS UITableView加索引条和搜索框Demo
最新推荐文章于 2021-10-17 14:31:40 发布