• IOS UITableView分组与索引分区实例


    1 #import <UIKit/UIKit.h>
    2 
    3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    4 
    5 @property (strong, nonatomic) UIWindow *window;
    6 
    7 
    8 @end
     1 #import "AppDelegate.h"
     2 #import "RootViewController.h"
     3 @interface AppDelegate ()
     4 
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 
    10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    12     // Override point for customization after application launch.
    13     self.window.backgroundColor = [UIColor whiteColor];
    14     
    15     RootViewController *root = [[RootViewController alloc] init];
    16     UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:root];
    17     self.window.rootViewController = navi;
    18     
    19     
    20     [self.window makeKeyAndVisible];
    21     return YES;
    22 }
    23 
    24 
    25 @end
    1 #import <UIKit/UIKit.h>
    2 
    3 @interface RootViewController : UIViewController
    4 
    5 @end
     1 #import "RootViewController.h"
     2 
     3 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
     4 {
     5     UITableView *_tableView;
     6     NSMutableDictionary *dataDic;
     7 }
     8 @end
     9 
    10 @implementation RootViewController
    11 
    12 - (void)loadView
    13 {
    14     [super loadView];
    15     _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
    16     _tableView.delegate = self;
    17     _tableView.dataSource = self;
    18     [self.view addSubview:_tableView];
    19 }
    20 
    21 - (void)viewDidLoad {
    22     [super viewDidLoad];
    23     NSArray *A = [NSArray arrayWithObjects:@"app",@"apple",@"alex",@"alert",@"alright",@"at",@"addic",@"awake",@"action",@"active", nil];
    24     NSArray *B = [NSArray arrayWithObjects:@"belance",@"beyond",@"bell",@"bill",@"bet",@"bety",@"bright",@"beaty",@"beat",@"bite",@"bit",@"bake",@"blake",@"basket",@"basketball",@"ball",@"black",@"blue", nil];
    25     NSArray *C = [NSArray arrayWithObjects:@"cake",@"cat",@"cap",@"cook",@"cooki",@"cate",@"cell",@"certain",@"city",@"clabe",@"clab",@"copy",@"cry", nil];
    26     NSArray *D = [NSArray arrayWithObjects:@"dirty",@"del",@"directly",@"dry",@"dull",@"delly",@"dute", nil];
    27     NSArray *E = [NSArray arrayWithObjects:@"elphance",@"every",@"else",@"emperty", nil];
    28     NSArray *F = [NSArray arrayWithObjects:@"fly",@"flash",@"flag",@"fate",@"felt",@"fill",@"fell",@"fall",@"font",@"fake",@"flour",@"ferver",@"fetech", nil];
    29     NSArray *G = [NSArray arrayWithObjects:@"girl",@"gipe",@"grap",@"gray",@"gay",@"gita",@"git",@"get",@"great",@"glass",@"glasses",@"good",@"google", nil];
    30     dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:A,@"A",B,@"B",C,@"C",D,@"D",E,@"E",F,@"F",G,@"G", nil];
    31 }
    32 
    33 #pragma mark -UITableView Delegate-
    34 
    35 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    36 {
    37     return [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    38 }
    39 
    40 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    41 {
    42     return [dataDic allKeys].count;
    43 }
    44 
    45 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    46 {
    47     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    48     NSString *key = keys[section];
    49     NSArray *array = dataDic[key];
    50     return [array count];
    51 }
    52 
    53 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    54 {
    55     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    56     NSString *key = keys[section];
    57     return key;
    58 }
    59 
    60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    61 {
    62     static NSString *identify = @"cell";
    63     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    64     if (cell == nil) {
    65         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    66     }
    67     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    68     NSString *key = keys[indexPath.section];
    69     NSArray *array = dataDic[key];
    70     NSString *content = array[indexPath.row];
    71     cell.textLabel.text = content;
    72     return cell;
    73 }
    74 
    75 - (void)didReceiveMemoryWarning {
    76     [super didReceiveMemoryWarning];
    77     // Dispose of any resources that can be recreated.
    78 }
    79 
    80 
    81 @end
  • 相关阅读:
    android studio你可能忽视的细节——启动白屏?drawable和mipmap出现的意义?这里都有!!!
    提升用户体验,你不得不知道的事儿——三种提醒框的微技巧
    【无私分享】修订版干货!!!一个炫酷的自定义日历控件,摆脱日历时间选择烦恼,纯福利~
    【无私分享】干货!!!一个炫酷的自定义日历控件,摆脱日历时间选择烦恼,纯福利~
    放弃安卓原生TimePicker,选择wheelView打造更漂亮的时间get,以及动态拉伸输入框布局,这些,这里都有!
    快速入手别人的安卓项目??你信我,不会想错过这个~~~
    打造android偷懒神器———RecyclerView的万能适配器
    打造android偷懒神器———ListView的万能适配器
    一个可随意定位置的带色Toast——开源代码Crouton的简单使用
    Python学习笔记3-文件的简单操作
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4758822.html
Copyright © 2020-2023  润新知