您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: