- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
// 设置代理
tableView.delegate = self;
tableView.dataSource = self;
// 注册重用池
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
[self.view addSubview:tableView];
[tableView release];
}
//返回每行高度 -- (默认44)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 8;
}
//每个row显示的内容(cell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
// 设置文字
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"";
cell.imageView.image = [UIImage imageNamed:@"Image"];
return cell;
}
//分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
//分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"A";
}
//分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 10)];
vv.backgroundColor = [UIColor greenColor];
return vv;
}
// 如果不想显示每个section的footer,则高度返回0
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// 当直接返回0的时候,系统会认为他是无效值,不会做任何更改,所以返回float类型的最小值
return CGFLOAT_MIN;
}
//返回右侧的索引数组
//与section相互呼应
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return @[@"A", @"B", @"C", @"D"];
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}