标签:style blog http ar io color os sp for
效果图:
点击后的效果图:
工程图:
代码:
viewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> { UITableView *myTableView; NSMutableArray *dataArray; NSMutableArray *indexArray; } @end
viewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 500)]; myTableView.dataSource=self; myTableView.delegate=self; //设置索引为橙色 myTableView.sectionIndexColor = [UIColor orangeColor]; [self.view addSubview:myTableView]; for(char c = ‘A‘; c <= ‘Z‘; c++ ) { if (!dataArray) { dataArray=[[NSMutableArray alloc]init]; } if (!indexArray) { indexArray=[[NSMutableArray alloc]init]; } [indexArray addObject:[NSString stringWithFormat:@"%c",c]]; [dataArray addObject:[NSString stringWithFormat:@"%c",c]]; [dataArray addObject:[NSString stringWithFormat:@"%c",c]]; [dataArray addObject:[NSString stringWithFormat:@"%c",c]]; } NSLog(@"----indexArray--%@",indexArray); NSLog(@"------dataArray----%@",dataArray); } #pragma -mark -UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [indexArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = [dataArray objectAtIndex:indexPath.section * 3 + indexPath.row]; return cell; } -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return indexArray; } -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { NSInteger count = 0; for(NSString *character in indexArray) { if([character isEqualToString:title]) { return count; } count ++; } return 0; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [indexArray objectAtIndex:section]; }
标签:style blog http ar io color os sp for
原文地址:http://www.cnblogs.com/yang-guang-girl/p/4171051.html