iOS滚动视图tableview,collectionviewtableview的那些事儿tableview和cell相关

TableView右边的索引效果实现:

2016-10-18  本文已影响549人  峰子1994

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添加了搜索标识

}

}

一系列的代理方法调用和实现就可以实现这个效果了:

上一篇 下一篇

猜你喜欢

热点阅读