#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
UI基础之TableView
最新推荐文章于 2023-06-06 19:39:55 发布