UITableView作为IOS开发最重要的控件,下面就来介绍下如果使用这个控件
(一)、介绍
1. 首先在代码中设置UITableView的数据源
如:self.tableView.dataSource = self;
2. 给数据源控制器实现UITableView的协议如:@interfaceViewController () <UITableViewDataSource>
@property (strong,nonatomic) IBOutletUITableView *tableView;
@end
3. 实现协议中的3个关键方法// 告诉tableView每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 告诉tableView一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
// 告诉tableView每一行显示什么数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
4.其他方法
// 返回每一组头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {};
// 返回每一组尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {};
注意点
(1).在 cellForRowAtIndexPath 行为中,如果 UITableViewCell 声明的时候用 initWithStyle 方式的话,可以使用子标题功能:
UITableViewCell *cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];
...
cell.detailTextLabel.text = @"...";
(2). - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; 这个方法可以不实现,默认返回1;