• ios-表视图-demo5-索引


    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)loadView
    {
        UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
        self.view = view;
        [view release];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ListData" ofType:@"plist"];
        _dataDic = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
        
        NSArray *keyArray = [NSArray arrayWithArray:[_dataDic allKeys]];
        
        // 排序
        _keyArray = [[keyArray sortedArrayUsingSelector:@selector(compare:)] retain];
        
        _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self; // 设置数据源
        _tableView.delegate   = self; // 设置委托
        [self.view addSubview:_tableView];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - TableView Datasource
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [_keyArray count];
    } // 表视图当中存在secion的个数,默认是1个
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    {
        NSArray *data = [_dataDic objectForKey:[_keyArray objectAtIndex:section]];
        return [data count];
    } // section 中包含row的数量
    
    - (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] autorelease];
        }
        // 给cell内容赋值
        
        NSArray *data = [_dataDic objectForKey:[_keyArray objectAtIndex:indexPath.section]];
        NSString *fontName = [data objectAtIndex:indexPath.row];
        cell.textLabel.text = fontName;
        cell.textLabel.font = [UIFont systemFontOfSize:18];
        
        return cell;
        
    } // 创建单元格
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return _keyArray[section];
    }
    
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return _keyArray;
    } // 返回索引的内容
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
        NSLog(@"index : %d title : %@", index, title);
        return index;//根据数组是索引内容,根据下表来取得跳转区域,默认也是跳转到下表坐标
    } // 选中时,跳转表视图
    
    #pragma mark - UITableViewDelegate
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 60;
    }
    
    - (void)dealloc
    {
        [_tableView release];
        _tableView = nil;
        [super dealloc];
    }
    
    @end
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    Java Socket通信实现私聊、群聊
    一套简单的web即时通讯——第二版
    一套简单的web即时通讯——第一版
    前后端API交互数据加密——AES与RSA混合加密完整实例
    跨境电商ERP中的自动化 3.平台订单自动发货
    跨境电商ERP中的自动化 2.平台商品和本地单品自动绑定
    跨境电商ERP中的自动化 1.平台订单自动同步至本地
    小特工具箱3.0版发布 春节优惠价99元/套
    河南农信移动支付解析
    win10 chrome 百分浏览器 centbrowser 收藏夹栏字体突然变小
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3695406.html
Copyright © 2020-2023  润新知