[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
























本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366428,如需转载请自行联系原作者
相关文章
|
5月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
53 0
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
152 0
|
缓存 算法 测试技术
iOS UITableView性能优化
iOS UITableView性能优化
iOS UITableView性能优化
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
309 0
|
iOS开发
iOS开发-关于UITableView去掉粘性的问题
iOS开发-关于UITableView去掉粘性的问题
77 0
|
iOS开发 开发者
iOS开发-简述UITableView中cell的重用问题
iOS开发-简述UITableView中cell的重用问题
181 0
|
Android开发 iOS开发 索引
仿IOS 带字母索引的滑轮控件
仿IOS 带字母索引的滑轮控件
仿IOS 带字母索引的滑轮控件
|
程序员 iOS开发
iOS 列表 UITableView 提速指南
从08年到现在开发过的iOS应用不计其数了,但是面试很多人的时候,发现依然很多同学在最基本的列表控件上懂得不够深,下面就结合各方面的资料进行再一次讲解。 我们都知道纯代码是效率最高的,但是在开发成本上已经越来越不如使用Storyboard性价比高,速度快,所以本文试图结合UIStoryboard来描述一整套方案。
174 0
iOS 列表 UITableView 提速指南
|
iOS开发 开发者
iOS开发中行高灵活可变的UITableView的性能优化(二)
iOS开发中行高灵活可变的UITableView的性能优化
377 0
iOS开发中行高灵活可变的UITableView的性能优化(二)
|
开发者 iOS开发
iOS开发中行高灵活可变的UITableView的性能优化(一)
iOS开发中行高灵活可变的UITableView的性能优化
218 0
iOS开发中行高灵活可变的UITableView的性能优化(一)