UI基础之TableView

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController
/*
 1、为UITableView设置数据源对象
 2、让数据源对象遵守UITableViewDataSource协议
 3、在数据源对象中实现UITableViewDataSource协议的方法(一般情况下会实现三个方法)
 
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //设置数据源对象的两种方式
    
    //1.代码的方式
    self.tableView.dataSource = self;
    //2.拖线的方式
}



//数据源方法

//告诉UITableView要显示几组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//告诉UITableView每组显示几条(几行)数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //根据不同的组,返回每组显示不同条数的数据
    if(section == 0){
        //亚洲
        return 3;
    }else if(section == 1){
        //非洲
        return 2;
    }else{
        return 1;
    }
    return 2;
}


//每组的每行显示什么样的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //创建单元格
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    if(indexPath.section == 0){
        //第一组,亚洲,三行
        if(indexPath.row == 0){
            cell.textLabel.text = @"中国";
            
        }else if(indexPath.row == 1){
            cell.textLabel.text = @"日本";
        }else{
            cell.textLabel.text = @"韩国";
        }
        
        
    }else if(indexPath.section == 1){
        //第二组,非洲
        if(indexPath.row == 0){
                   cell.textLabel.text = @"b南非";
                   
               }else{
                   cell.textLabel.text = @"索马里";
               }
        
    }else {
        //第三组,欧洲
        cell.textLabel.text = @"荷兰";
    }
    return cell;
}


//每一组的组标题显示什么
-(NSString *)tableView:(UITableView*)tableView tetleForHeaderInsection:(NSInteger)section{
    //根据当前组的索引section,返回不同组的标题
    if(section == 0){
        return @"亚洲";
    }else if(section == 1){
        return @"非洲";
        
    }else{
        return @"欧洲";
    }
}
//每一组的组尾即组描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    //根据当前组的索引section,返回不同组的描述信息
    if(section == 0){
        return @"亚洲,yazhou";
    }else if(section == 1){
        return @"非洲,feizhou";
    }else{
        return @"欧洲,ouzhou";
    }
}


//告诉UITableView每一组的每一行显示什么单元格内容
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //创建一个单元格对象并返回
//
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
//
//
//    //为单元格指定数据
//    cell.textLabel.text = @"hello";
//    return cell;
//}

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Αиcíеиτеǎг

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值