UITableView with index and UITableView with index
:
After clicking:
Engineering Drawing:
Code:
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; // set the index to orange 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) values :( 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 failed: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: Repeated 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 Ister tostring: title]) {return count;} count ++ ;} return 0;}-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [indexArray objectAtIndex: section];}