Tableview的右邊的索引的實現我們改如何去實現呢? 最近一個好友剛好弄這個我就研究了在這里給大家分享下這個效果了:
這個實現好久都不用了都快忘記了:我直接上代碼了:
這個是數據源:
-(void)initDataSource{
NSArray*array =@[@"登記",@"大奔",@"周傅",@"愛德華",@"((((",@"啦文琪羊",@"s文強",@"過段時間",@"等等",@"各個",@"宵夜**",@"***",@"貝爾",@"*而結婚*",@"返回***",@"你還",@"與非門*",@"是的",@"*模塊*",@"*沒做*",@"俄文",@"*#咳嗽",@"6",@"fh",@"C羅",@"鄧肯"];
NSArray*indexArray = [arrayarrayWithPinYinFirstLetterFormat];
self.dataArray= [NSMutableArrayarrayWithArray:indexArray];
[self.myTableViewreloadData];
}
#pragma mark - uitableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return[self.dataArraycount];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
if(section ==0) {
return1;
}else{
NSDictionary*dict =self.dataArray[section];
NSMutableArray*array = dict[@"content"];
return[arraycount];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"resiter_cell"];
NSDictionary*dict =self.dataArray[indexPath.section];
NSMutableArray*array = dict[@"content"];
cell.textLabel.text= array[indexPath.row];
NSLog(@"cell.textLabel.text:%@",cell.textLabel.text);
cell.textLabel.textColor= [UIColorcolorWithRed:0.10green:0.68blue:0.94alpha:1.0];
returncell;
}
//定義頭部的view
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
//自定義header標題
UIView*myView = [[UIViewalloc]init];
myView.backgroundColor= [UIColorcolorWithRed:0.10green:0.68blue:0.94alpha:0.7];;
UILabel*titleLabel = [[UILabelalloc]initWithFrame:CGRectMake(10,0,90,22)];;
titleLabel.textColor= [UIColorwhiteColor];
NSString*title =self.dataArray[section][@"firstLetter"];
titleLabel.text= title;
[myViewaddSubview:titleLabel];
returnmyView;
}
#pragma mark tableView的索引
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
NSDictionary*dict =self.dataArray[section];
NSString*title= dict[@"firstLetter"];
NSLog(@"title:%@",title);
returntitle;
}
//添加索引爛標題數組
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
NSMutableArray*resultArray =[NSMutableArrayarrayWithObject:UITableViewIndexSearch];
for(NSDictionary*dictinself.dataArray) {
NSString*title = dict[@"firstLetter"];
[resultArrayaddObject:title];
}
returnresultArray;
}
//點擊索引欄標題時執行
- (NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
//這里是為了指定索引index對應的是哪個section的,默認的話直接返回index就好。其他需要定制的就針對性處理
if([titleisEqualToString:UITableViewIndexSearch])
{
[tableViewsetContentOffset:CGPointZeroanimated:NO];//tabview移至頂部
returnNSNotFound;
}
else
{
return[[UILocalizedIndexedCollationcurrentCollation]sectionForSectionIndexTitleAtIndex:index] -1;// -1添加了搜索標識
}
}
一系列的代理方法調用和實現就可以實現這個效果了: