#import "ViewController.h"
//遵守协议
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
{
NSArray *array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建UITableView
UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain];
[self.view addSubview:tab];
//创建行高
tab.rowHeight = 160;
//选择代理
tab.delegate = self;
tab.dataSource = self;
//隐藏滚动条
tab.showsVerticalScrollIndicator = NO;
tab.showsHorizontalScrollIndicator = NO;
//添加表头视图
UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_01.png"]];
tab.tableHeaderView = imgV;
//数据源数组
array = [NSArray arrayWithObjects:@"鲁班",@"墨子",@"火舞",@"甄姬", nil];
}
//代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//行数
return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建静态复用标识符
static NSString *identifier = @"cell";
//根据标识符从重用池取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//如果没有取到就创建新cell
if(cell == nil)
{
NSLog(@"进来了");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
//对cell进行赋值
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.detailTextLabel.text = @"看我大招";
cell.imageView.image = [UIImage imageNamed:@"icon_00.png"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//使用自己想用的view进行赋值
//cell.accessoryView = [[UIView alloc] init];
return cell;
}
//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 160;
}
//控制分区的代理方法
//分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
//调整分区的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70;
}
//分区的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
return @"战士";
}else if(section ==1)
{
return @"法师";
}else
{
return @"坦克";
}
}
//自定义分区
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 140)];
view.backgroundColor = [UIColor redColor];
return view;
}
@end
———效果:点击时变成灰色,松开时回复原状态
//选中某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消当前选中的那一行
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//取消上一次选中的那一行
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}