IOS学习之——TableView基础

//
//  MyViewController.m
//

#import "MyViewController.h"

@interface MyViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    //记住tableView 一定要设置数据源对象
    tableView.dataSource = self;
    //设置tableView 的delegate
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

//tableView  需要 三问 才能显示内容
//三问 是dataSource协议中的方法
//第1问的方法可以选择实现或不实现,如果不实现默认是1个分区
//第2问和第3问的方法,都是必须要实现的
//1.有个几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
//2.每个分区有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return  10; //0分区 10行
    }else if(section == 1) {
        return 5; //1分区 5行
    }else  {
        return 8; // 其他分区都是8行
    }
}
//3.每行长什么样子
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //indexPath 参数中保存 当前你要设置Cell的 分区号 和 行号
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    switch (indexPath.section) {
        case 0:
            if(indexPath.row == 0) {
                cell.textLabel.text = @"你妹";
            }else {
                cell.textLabel.text = @"呵呵";
            }
            break;
        case 1:
            if (indexPath.row == 1) {
                cell.textLabel.text = @"是不是说重了?";
            }else {
                cell.textLabel.text = @"你管的着吗?";
            }
            break;
        case 2:
            cell.textLabel.text = @"Bull Shit";
            break;
    }
    return cell;
}
//1答, 点中了做什么事
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点中了 %ld 分区 %ld 行",indexPath.section, indexPath.row);
}



//设置分区头的 文本内容
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"这是第%ld个分区头",section];
}
//设置分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值