tableview

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值