UI4

it2024-11-09  9

// AppDelegate.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. ViewController *root = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root]; self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }

 

// // ViewController.h // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @property (strong, nonatomic)UITableView *tableView; @property (strong, nonatomic)NSMutableArray *dataList; @end // // ViewController.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self creatUI]; [self creatDataSource]; } - (void)creatDataSource { _dataList = [NSMutableArray array]; for (int i='A'; i<='Z'; i++) { NSInteger count = arc4random()%10+1; NSMutableArray *array = [NSMutableArray array]; for (int j=0; j<count; j++) { NSString *name = [NSString stringWithFormat:@"人物:%d", j+1]; [array addObject:name]; } [self.dataList addObject:array]; } } - (void)creatUI { _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; //设置代理 self.tableView.delegate = self; self.tableView.dataSource = self; self.automaticallyAdjustsScrollViewInsets = YES; self.view.backgroundColor = [UIColor whiteColor]; } #pragma mark ---UITableViewDataSource--- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataList.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.dataList objectAtIndex:section] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"cell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellId]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId]; } cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.text = [[self.dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringWithFormat:@"第%c分区", (char)(section+'A')]; } //设置分区头标题的高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; } //创建分区标题数组 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSMutableArray *array = [NSMutableArray array]; [array addObject:UITableViewIndexSearch]; for (int i='A'; i<='Z'; i++) { NSString *str = [NSString stringWithFormat:@"%c", (char)i]; [array addObject:str]; } return array; } //切换到指定的分区 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return index-1; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

 

转载于:https://www.cnblogs.com/0515offer/p/4645991.html

最新回复(0)