您的位置:首页 > 其它

tableview

2019-03-18 22:00 120 查看

1.tableview有代理和源,主要通过代理和源对其进行设置,有很多系统定义的函数可以为其服务
2.tableview就是选项表,就跟外卖那种一样

#import "ViewController.h"
//设置代理协议
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSArray *arr;//设置每一行的显示内容
NSArray *arr1;
NSArray *arr2;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arr=[[NSArray alloc] initWithObjects:@"A",@"B",@"C", nil];
arr1=[[NSArray alloc] initWithObjects:@"A1",@"B1",@"C1", nil];
arr2=[[NSArray alloc] initWithObjects:@"A2",@"B",@"C2", nil];

UITableView *mytableviw = [[UITableView alloc] init];
mytableviw.frame=self.view.frame;
mytableviw.delegate=self;//代理源
mytableviw.dataSource=self;//数据源
mytableviw.tableFooterView=[[UIView alloc] init];
[self.view addSubview:mytableviw];
}

设置一共有多少行

//两个必须要设置的代理方法:一个设置一共有多少个数据,另一个显示每个数据的内容
//显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section==0){//section设置来自哪一分组
return arr.count;
}
else if(section==1){
return arr1.count;
}
return arr2.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;//设置多少组数据
}

设置每个对象的内容

//列表中每一个view对象,设置每一个view的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"mycell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
//    cell.textLabel.text=[NSString stringWithFormat:@"第%ld行数据",indexPath.row];
if(indexPath.section==0){
cell.textLabel.text=arr[indexPath.row];
}
else if(indexPath.section==1){
cell.textLabel.text=arr1[indexPath.row];
}
else{
cell.textLabel.text=arr2[indexPath.row];
}

return cell;
}

设置点击事件
添加表头和表尾

//点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger cellnum=indexPath.row;
//    NSLog(@"我点击了第%ld行",cellnum);
NSLog(@"我点击了第%@",arr[cellnum]);

}

//添加一个表头
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *header=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];//这个高度不会影响控件高度要单独设置代理
header.backgroundColor=[UIColor blueColor];
header.text=[NSString stringWithFormat:@"%ld",section];
header.textColor=[UIColor whiteColor];
return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}

////添加一个表尾
//-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if(section!=2) return nil;
//    UILabel *header=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];//这个高度不会影响控件高度要单独设置代理
//    header.backgroundColor=[UIColor blueColor];
//    header.text=@"这是尾部";
//    header.textColor=[UIColor whiteColor];
//    return header;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 50;
//}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: