[IOS]UITableView分区+索引显示

效果:

步骤:

1.创建一个ViewController,New File->Cocoa Touch->Objective-C class->Class:ViewController,Subclass of:UIViewController

2.打开xib,在view中添加TableView,并将TableView的两个属性拖到File's Owner中,

可以设置tableview的分区样式,选择style

3.ViewController.h:

#import <UIKit/UIKit.h>
@interface newViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property(retain,nonatomic)NSDictionary *dic;
@property(retain,nonatomic)NSArray *keys;
@end

4.ViewController.m:

#import "newViewController.h"

@interface newViewController ()

@end

@implementation newViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    self.dic = [NSDictionary dictionaryWithContentsOfFile:path];

    self.keys = [self.dic allKeys];
    self.keys = [self.keys sortedArrayUsingSelector:@selector(compare:)];
}

//tableView有多少分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.keys count];
}

//每个分区对应多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = self.keys[section];//通过第几个分区拿到对应的key
    NSArray *arr = [self.dic objectForKey:key];//通过这个key获得对应的Array
    return [arr count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"str";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    int section = [indexPath section];
    int row = [indexPath row];

    NSString *key = self.keys[section];
    NSArray *arr = [self.dic objectForKey:key];
    cell.textLabel.text = arr[row];
    return cell;
}
//在每个分区上显示什么内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.keys[section];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keys;
}

- (void)dealloc {
    [_tableView release];
    [self.keys release];
    [self.dic release];
    [super dealloc];
}
@end
时间: 2024-09-20 07:37:30

[IOS]UITableView分区+索引显示的相关文章

iOS UITableView代理方法详解

IOS UITableView的代理方法详解 一.补充 在上一篇博客中,http://my.oschina.net/u/2340880/blog/404605,我将IOS中tableView(表视图)的一些常用方法总结了一下,这篇将tableView的代理方法作了总结,对上一篇博客进行了补充. 二.UITableViewDataSourc(数据源代理) 1.必须实现的回调方法 返回每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView nu

iOS UITableView 与 UITableViewController实例详解_IOS

很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名. UITableView 对象虽然只能显示一行数据,但是没有行数限制. •编写新的应用程序 JXHomepwner 应用 创建应用,填写基本信息 •UITableViewController UITableView 是视图.我们知道 模型-视图-控制器(Model-View-Controller),他是我们必须遵守的一种

SQL Server已分区索引的特殊指导原则(3)

一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的交流. SQL Server 解读[已分区索引的特殊指导原则](1)- 索引对齐 SQL Server 解读[已分区索引的特殊指导原则](2)- 唯一索引分区 二.解读 [对非聚集索引进行分区] "对唯一的非聚集索引进行分区时,索引键必须包含分区依据列.对非唯一的非聚集索引进行分区时,默认情况下 S

SQL Server已分区索引的特殊指导原则(2)- 唯一索引分区

一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的交流. 二.解读 [对唯一索引进行分区] "对唯一索引(聚集或非聚集)进行分区时,必须从唯一索引键使用的分区依据列中选择分区依据列.此限制将使 SQL Server 只调查单个分区,以确保表中不存在重复的新键值.如果分区依据列不可能包含在唯一键中,则必须使用 DML 触发器,而不是强制实现唯一性.&

SQL Serve已分区索引的特殊指导原则(1)- 索引对齐

一.前言 在MSDN上看到一篇关于SQL Server 表分区的文档:已分区索引的特殊指导原则,如果你对表分区没有实战经验的话是比较难理解文档里面描述的意思.这里我就里面的一些概念进行讲解,方便大家的交流. 二.解读 "索引要与其基表对齐,并不需要与基表参与相同的命名分区函数.但是,索引和基表的分区函数在实质上必须相同,即: 1) 分区函数的参数具有相同的数据类型: 2) 分区函数定义了相同数目的分区: 3) 分区函数为分区定义了相同的边界值." 下面我们进行测试: --1.创建文件组

oracle的分区索引和全局索引

分区索引就是在所有每个区上单独创建索引,它能自动维护,在drop或truncate某个分区时不影响该索引的其他分区索引的使用,也就是索引不会失效,维护起来比较方便,但是在查询性能稍微有点影响. 建立分区索引: create index idx_ta_c2 on ta(c2) local (partition p1,partition p2,partition p3,partition p4); 或者 create index idx_ta_c2 on ta(c2) local ; 另外在crea

PL/SQL Developer导出分区索引脚本

不准确分析及规避方法 1 案例回访 硬件环境:IBM XSERIES 3650 操作系统:Windows2003标准版+Sp02 数据库版本:Oracle9.2.0.1 PL/SQL Developer版本:7.0.2.1076 由于业务需要,需先drop一张分区表T_SMS,再重建此表,要求表结构.索引等完全一致.但当时开局版本一时无法找到,故决定利用PL/SQL Developer工具"View SQL"获取到的SQL语句来重建分区表.(此分区表按天进行分区,且由于每天数据量庞大,

Oracle Local VS Global分区索引简介

在Oracle中,索引和表一样也可以分区.有两种类型的分区索引,本地分区索引(Local )和全局分区索引(Global). 1.本地索引(Local) 本地分区索引使用 LOCAL关键字创建,其分区边界与表相同(即与每个表分区相关联都有一个索引分区),下面 是一个本地分区索引的例子: create table sales_par partitioned by range (year) ( partition p_2009 values less than (2010) partition p_

Oracle中一个通过添加本地分区索引提高SQL性能的案例

今天接到同事求助,说有一个select query,在Oracle上要跑一分多钟,他希望能在5s内 出结果,该sql如下: Select /*+ parallel(src, 8) */ distinct src.systemname as systemname , src.databasename as databasename , src.tablename as tablename , src.username as username from <strong>meta_dbql_tabl