zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    用错每日活跃用户(DAU)这个指标,无异于挖坑给自己跳
    自我修复型设计-常用的架构设计原则
    UParams
    nodejs的使用场景
    问题
    GitHub Copilot可对整行或全部代码给出AI分析建
    Your AI pair programmer
    飞冰React框架如何配置懒加载
    【630】keras 实现多输出模型
    【629】图像增强(imgaug 包)
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4758822.html
Copyright © 2011-2022 走看看