UITableView的简单使用
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//建立数组并放入元素,作为第一层的数据
-(NSArray *)firstSection{
return @[@"第一行",@"第二行",@"第三行"];
}
//tableview中section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//没个section有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0:
return 3;
case 1:
return 1;
case 2:
return 2;
default:
return 0;
}
}
//每个section的头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 15;
}
return 18;
}
//每个section的底部高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 2) {
return 100;
}
return CGFLOAT_MIN;
}
//每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 52;
}else{
return 48;
}
}
//放入数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuseIdetify = @"SvTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify];
switch (indexPath.section) {
case 0:{
cell.textLabel.text = [self firstSection][indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
}
case 1:{
cell.textLabel.text = @"第四行";
break;
}
case 2:{
if (indexPath.row == 0) {
cell.textLabel.text = @"第五行";
}else{
cell.textLabel.text = @"第六行";
}
break;
}
default:
break;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//底部建立一个自定义的View
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if (section == 2) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 34, CGRectGetWidth([UIScreen mainScreen].bounds)-50, 50)];
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"点击" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:18]];
[button.layer setCornerRadius:5];
[button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 100)];
[view addSubview:button];
return view;
}
return nil;
}
//每行的点击事件 提示点击的行数
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *buttonRow = [[NSString alloc]initWithFormat:@"你选中的是%li行",indexPath.row];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:buttonRow delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
}
//按钮的点击
-(void)didTapButton:(id)sender{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你已选中" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
主要的就是UITableView的几种方法,尤其是footerView的使用