UITableView 协议
2016-05-31 00:00
447 查看
摘要: 表格视图的索引
#import <UIKit/UIKit.h> @interface VCRoot : UIViewController < //数据源协议 UITableViewDataSource, //需要使用事件协议 UITableViewDelegate> { UITableView* _tableView ; NSMutableArray* _arrayData ; } @end #import "VCRoot.h" @interface VCRoot () @end @implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped] ; _tableView.delegate = self ; _tableView.dataSource = self ; [self.view addSubview:_tableView] ; //创建数组 _arrayData = [[NSMutableArray alloc] initWithCapacity:0] ; for (int i = 0 ; i < 5; i++) { NSString* str = [NSString stringWithFormat:@"第%d行",i+1] ; [_arrayData addObject:str] ; } [_tableView release] ; _tableView.backgroundColor = [UIColor clearColor]; //[_arrayData release] ; } //析构函数 -(void) dealloc { [_arrayData release] ; [super dealloc] ; } //组数 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 10 ; } //单元格行数 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _arrayData.count ; } -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* strID = @"ID" ; //使用单元格复用机制创建cell对象 UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:strID] ; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID] ; } NSString* str = [NSString stringWithFormat:@"第%d组,%@",indexPath.section+1,_arrayData[indexPath.row]]; cell.backgroundColor = [UIColor clearColor]; cell.textLabel.text = str; return cell ; } //设置单元格的高度 //参数一:数据视图 //参数二:单元格位置索引 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40 ; if (indexPath.row % 2 == 0) { return 40 ; } return 100 ; } //返回每组尾部的高度 //参数一:数据视图 //参数二:当前的第几组数 //默认值为40 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 40 ; } //组与组之间的空隙 //返回每组的头部高度 //参数一:数据视图 //参数二:当前的第几组数 //默认值为40 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 80 ; } //获取每组头部的文字信息 //参数一:tableview //参数二:对应的组数 -(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString* str = [NSString stringWithFormat:@"第%d组头部",section+1] ; return str ; } //获取每组尾部的文字信息 //参数一:tableview //参数二:对应的组数 -(NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { NSString* str = [NSString stringWithFormat:@"第%d组结束",section+1] ; return str ; } //获取索引列表数据数组 -(NSArray*) sectionIndexTitlesForTableView:(UITableView *)tableView { //创建一个新数组, //用来保存右侧小索引图标的文字 NSMutableArray* arrayIndex = [[NSMutableArray alloc] init] ; [arrayIndex addObject:@"开始"] ; for (int i = 'A' ; i < 'I' ; i++) { NSString* str = [NSString stringWithFormat:@"%c",i] ; [arrayIndex addObject:str] ; } [arrayIndex addObject:@"#"] ; _tableView.sectionIndexBackgroundColor = [UIColor clearColor];//索引条背景色 //延迟释放 return [arrayIndex autorelease] ; } //点击索引图标文字时,跳转到的位置 //返回值:跳转到的组的索引section //参数一:数据视图 //参数二:点击文字的字符串内容 //参数三:点击的文字所在数组中的索引 -(NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { if ([title isEqualToString:@"开始"] == YES) { return -1 ; } if ([title isEqualToString:@"结束"] == YES) { return 9 ; } return index-1 ; } @end
相关文章推荐
- Key-Value Coding
- Key-Value Observing
- 【欧拉函数表】POJ2478-Farey Sequence
- OC中限制UITextView的最大字数的实现
- Creational Patterns Part 4/5: Builder Pattern
- 拖动UIScrollView时的delegate调用调用顺序
- UINavigationController和UITabBarController合用
- 导入错误,Gradle version 2.10 is required. Current version is 2.8
- PrimusUI
- UIView属性的动画
- APUE(3)---文件I/O (1)
- iOS之UIView的用法
- 如何通过jd-gui反编译后使用BCCompare比较源码忽略不重要差异时不显示红色
- iOS学习- 06 UI设计 Sketch使用
- String、StringBuffer和StringBuilder的区别
- use prebuild static lib on another android project
- Leetcode之unique-binary-search-trees
- Xamarin.IOS之将UIImage保存到本地
- HDU 4746 Mophues【莫比乌斯反演】
- HDU 4746 Mophues【莫比乌斯反演】