UITableView的简单使用
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSArray *_array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = @[@"q",@"w",@"e",@"r",@"t",@"y",@"u",@"",@"i",@"o",@"a"];
[self addTableViewToView];
}
- (void)addTableViewToView
{
// 1. 获取tableView对象
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
// 2. 设置数据源和代理
// 设置数据源:负责tabelView显示什么数据
// 设置代理: 可以获取用户的点击事件
tableView.dataSource = self;
tableView.delegate = self;
// 3. 把对象添加到界面
[self.view addSubview:tableView];
}
#pragma mark tableView 显示多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
#pragma mark 每组有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ID = @"CELL";
// 1.通过ID去缓存池中查找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2. 如果缓存池中没有这个cell对象,那么就创建cell对象
if (cell == nil) {
// 不显示副标题
// UITableViewCellStyleDefault,
// 主标题在左,副标题在右
// UITableViewCellStyleValue1,
// 不显示图片,主标题蓝色字体
// UITableViewCellStyleValue2,
// 主标题在上,副标题在下
// UITableViewCellStyleSubtitle
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
NSLog(@"%ld---%ld",indexPath.row, indexPath.section);
// 设置主标题
cell.textLabel.text = _array[indexPath.row];
// 设置副标题
cell.detailTextLabel.text = @"我是副标题";
cell.imageView.image = [UIImage imageNamed:@"0"];
return cell;
}
@end
UITableView的简单使用.png
UITableView详细属性设置
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addTableViewToView];
}
- (void)addTableViewToView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
// 设置代理和数据源
tableView.dataSource = self;
tableView.delegate = self;
// tableView.rowHeight = 10;
// tableView.tableFooterView
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
#pragma mark 返回每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// section 组
if (section == 0) {
return 10;
}else if (section == 1){
return 5;
}else{
return 15;
}
}
#pragma mark 返回有多少组-- 如果这个方法我们没有实现,那么默认为一组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
#pragma mark 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rID = @"ID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:rID];
}
// if (indexPath.section == 0) {
// cell.backgroundColor = [UIColor redColor];
// }else if (indexPath.section == 1){
// cell.backgroundColor = [UIColor yellowColor];
// }else{
// cell.backgroundColor = [UIColor purpleColor];
// }
cell.textLabel.text = @"Hello word!";
return cell;
}
#pragma mark 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.section == 0) {
// if (indexPath.row == 3) {
// return 200;
// }
// }
return 100;
}
#pragma mark 组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"童鞋";
}else if (section == 1){
return @"欠我钱的";
}else{
return @"大学童鞋";
}
}
#pragma mark 设置尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"titleForFooter";
}
#pragma mark 设置头标题控件的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
#pragma mark 设置尾标题控件的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100;
}
#pragma mark 自定义头标题控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 10, 200, 80)];
imageView.image = [UIImage imageNamed:@"2222"];
imageView.backgroundColor = [UIColor redColor];
return imageView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 10, 200, 80)];
imageView.image = [UIImage imageNamed:@"2222"];
imageView.backgroundColor = [UIColor redColor];
return imageView;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld---%ld",indexPath.row,indexPath.section);
}
@end
UITableView详细属性设置.png
网友评论