IOS开发|IOS开发 UITableView基础

本节学习内容:
1.UITableView基本概念
2.UITableView基本创建
3.UITableView的基本使用
【UITableView属性】
dataSource:数据代理对象
delegate:普通代理对象
numberOfSectionsInTableView:获取组数协议
numberOfRowsInSection:获取行数协议
cellForRowAtIndexPath:创建单元格协议
【ViewController.h】
#import
@interface
//UITableViewDelegate:实现数据视图的普通协义,数据视图的普通事件处理
//UITableViewDataSource 实现数据视图的数据代理协议,处理数据视图的数据代理
viewController:UIViewController

//定义一个数据视图对象,数据视图用来显示大量相同格式的信息的视图,例如:电话通记录,朋友圈信息...
UITableView* _tableView;

@end
【ViewController.m】
-(void)viewDidLoad{
[super viewDidLoad];
//创建数据视图,参数1:数据视图的位置,参数2:数据视图的峁格,UITableViewStylePlain表示普通风,UITableViewStyleGruped表示分组风格
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//设置数据视图的代理对象
_tableView.delegate=self;

//设置数据视图的数据源对象
_tableView.dataSource=self;

【self.view addSubview:_tableView】
}
//获取每组元素的个数(行数),必须要实现的协义函数,程序在显示数据视图时会调用此函数,返回值:表示每组元素的个数,参数1:数据视图对象本身,参数2:那一组需要的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
//设置数据视图的组数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//创建单元格对象函数
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString* cellStr=@"cell";
UITableViewCell* cell=[_tableView dequeueReusableCellWithIdentifier:cellStr];
if(cell ==nil){
//创建一个单元格对象,参数一:单元格的样式,参数二:单元格的复用标记

cell=[[UITableViewCell alloc]iniWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
}
NSString* str=[NSSTring stringWithFormat:@"每%组,第%d行",indexPath.section,indexPath.row];
//将单元格的主文字内容赋值
cell.textLabel.text=str;
return cell;
}
【IOS开发|IOS开发 UITableView基础】

IOS开发|IOS开发 UITableView基础
文章图片
代码实现

    推荐阅读