UITableView 预习

//想要在页面铺出数据的方法

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

self.arr = [NSMutableArray arrayWithObjects:@"大鸭梨", @"小鸭梨", @"中鸭梨",@"大大鸭梨",@"中大鸭梨",@"超级大鸭梨",@"无敌大鸭梨", nil];

}

return self;

}


//设置tableView的属性

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

UITableView *myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStyleGrouped)];

myTableView.backgroundColor = [UIColor redColor];

[self.view addSubview:myTableView];

[myTableView release];

myTableView.rowHeight = 100;

//签订两个协议

myTableView.delegate = self;

myTableView.dataSource = self;

}

//一共有几个section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 3;

}

//必须写的方法 section上有几行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (section % 2 == 0) {

return 5;

} else {

return self.arr.count;

}

}

//必须写的方法 看见indexpath就是想写出每行上面的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *resue = @"resue";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];

if (!cell) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:resue]autorelease];

}

cell.textLabel.text = self.arr[indexPath.row];

return cell;

}

//分别设置行的 宽度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.section == 0) {

return 20;

}

if (indexPath.section == 2) {

if (indexPath.row == 0) {

return 150;

}

return 50;

}

return 50;

}

//给每个section上面设置 头- (nullable NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView

{

return @[@"A",@"B",@"C"];

}

//给section的头上面取个小名

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [NSString stringWithFormat:@"%ld",section];

}

你可能感兴趣的:(UITableView 预习)