为什么80%的码农都做不了架构师?>>>
一 UITableView简介
UITableView用于表格数据展示,是使用频率最高的控件。UITableView继承自UIScrollView,具有UIScrollView的所有特性。
二 UITableView的两种样式
2.1 UITableViewStylePlain 2.2 UITableViewStyleGrouped
三 UITableView数据展示
UITableView需要数据源(dataSource)来显示数据,实现UITableViewDataSource协议的对象,都可以是UITableView的数据源。通过协议中的方法,UITableView显示相应的行数以及每行显示的数据。
3.1 调用数据源的以下方法获取一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
3.2 调用数据源的以下方法获取每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.3 调用数据源的以下方法获取每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
四 UITableViewCell
UITableView每行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每行
UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可以通过设置UITableViewCell的accessoryType来显示一些辅助指示视图,辅助指示视图用于表示动作的图标。
4.1 辅助指示视图
UITableViewCell的accessoryType值 | 图标样式 |
UITableViewCellAccessoryNone(默认) | 不显示辅助指示视图 |
UITableViewCellAccessoryDisclosureIndicator | ![]() |
UITableViewCellAccessoryDetailDisclosureButton | ![]() |
UITableViewCellAccessoryCheckmark | ![]() |
UITableViewCellAccessoryDetailButton | ![]() |
注:还可以通过UITableViewCell的accessoryView属性来自定义辅助指示视图
4.2 contentView 的子视图
//4.2.1 textLabel
@property (nonatomic, readonly, retain) UILabel *textLabel;//4.2.2 detailTextLabel
@property (nonatomic, readonly, retain) UILabel *detailTextLabel;//4.2.3 imageView
@property (nonatomic, readonly, retain) UIImageView *imageView;
4.3 UITableViewCell的UITableViewCellStyle属性
UITableViewCellStyle用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置
UITableViewCellStyle的值 | 子视图样式 |
UITableViewCellStyleDefault | ![]() |
UITableViewCellStyleSubtitle | ![]() |
UITableViewCellStyleValue1 | ![]() |
UITableViewCellStyleValue2 | ![]() |
五 界面简单实现
提供car类,创建car类的数组,用于展示.
5.1 代码
--car类
@interface GMCar : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, strong) NSArray *subCars;
@end
#import "ViewController.h"
#import "GMCar.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
@end@implementation ViewController- (void)viewDidLoad {//1.UITableView 设置//1.1 设置数据代理self.tableView.dataSource = self;//1.2 设置frameself.tableView.frame = self.view.frame;//1.3 设置代理self.tableView.delegate = self;//2.UITableView加入veiw[self.view addSubview:self.tableView];
}#pragma mark - 数据加载
/*** 调用数据源以下方法获取一共有多少组数据** @param tableView <#tableView description#>** @return <#return value description#>*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return self.array.count;
}/*** 调用数据源方法获取每一组有多少行数据** @param tableView <#tableView description#>* @param section <#section description#>** @return <#return value description#>*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.subCars.count;
}/*** 调用数据源方法获取每一行显示什么内容** @param tableView <#tableView description#>* @param indexPath <#indexPath description#>** @return <#return value description#>*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//1.创建cellUITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];//1.1 设置accessoryType// UITableViewCellAccessoryNone, // don't show any accessory view// UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track// UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks// UITableViewCellAccessoryCheckmark, // checkmark. doesn't track// UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. trackscell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//2.取出数据GMCar *car = self.array[indexPath.section];NSString *name =car.subCars[indexPath.row];//3.设置数据cell.textLabel.text =name;return cell;
}/*** section组显示标题** @param tableView <#tableView description#>* @param section <#section description#>** @return <#return value description#>*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.title;
}/*** section组显示描述** @param tableView <#tableView description#>* @param section <#section description#>** @return <#return value description#>*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.desc;
}#pragma mark - UITableViewDelegate
/*** table 每一行的高度** @param tableView <#tableView description#>* @param indexPath <#indexPath description#>** @return <#return value description#>*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 20.0;
}/*** section组Header的高度** @param tableView <#tableView description#>* @param section <#section description#>** @return <#return value description#>*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{NSLog(@"height");return 30.0;
}/*** section组Footer的高度** @param tableView <#tableView description#>* @param section <#section description#>** @return <#return value description#>*/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{return 30.0;
}#pragma mark - 懒加载
-(UITableView *)tableView
{if (!_tableView) {//UITableViewStyleGrouped样式_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];//UITableViewStylePlain 样式//_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];}return _tableView;
}-(NSArray *)array
{if (!_array) {_array = [[NSArray alloc]init];GMCar *c1 = [[GMCar alloc] init];c1.title = @"德国品牌";c1.desc = @"世界一流品牌";c1.subCars = @[@"奥迪" , @"宝马"];GMCar *c2 = [[GMCar alloc] init];c2.title = @"日本品牌";c2.desc = @"实用价值高";c2.subCars = @[@"丰田" , @"本田"];GMCar *c3 = [[GMCar alloc] init];c3.title = @"欧美品牌";c3.desc = @"高逼格";c3.subCars = @[@"劳斯莱斯" , @"布加迪", @"兰博基尼"];_array = @[c1, c3, c2];}return _array;
}#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{return YES;
}
@end
5.2 展示
UITableViewStylePlain 样式 UITableViewStyleGrouped 样式
UITableViewCell的accessoryType值为UITableViewCellAccessoryDetailDisclosureButton