iOS开发基础-UITableView控件简单介绍

时间:2023-03-08 22:46:00

   UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动。

   UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么内容等。凡是遵守 UITableViewDataSource 协议的Objc对象,都可以是 UITableView 的数据源。

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  返回共有多少组数据。

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  返回每一组有多少行数据。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  返回每一行显示的内容。

   - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section  返回各组底部显示的标题。

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  返回各组头部显示的标题。

实例

  新建一个Single View Application,命名为TableViewDemo,首先让 ViewController 类遵守 UITableViewDataSource 协议,并声明一个 UITableView 属性,如下所示:

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

  将Table View视图拖到storyboard中,并将其与 tableView 属性建立关联。

  接下来设置 tableView 的数据源,修改 viewDidLoad 方法:

 //ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
}

  重写 UITableViewDataSource 协议的方法:

 #pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%s", __FUNCTION__);
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%s", __FUNCTION__);
if (section == ) {
return ;
} else {
return ;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section == ) {
if (indexPath.row == ) {
cell.textLabel.text = @"奥迪";
} else if (indexPath.row == ) {
cell.textLabel.text = @"宝马";
}
} else if (indexPath.section == ) {
if (indexPath.row == ) {
cell.textLabel.text = @"本田";
} else if (indexPath.row == ) {
cell.textLabel.text = @"丰田";
} else if (indexPath.row == ) {
cell.textLabel.text = @"马自达";
}
}
return cell;
} - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == ) {
return @"高端大气上档次";
} else {
return @"还不错";
}
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == ) {
return @"德系品牌";
} else {
return @"日韩品牌";
}
}

   UITableViewCell 有一个 UITableViewCellStyle 属性,用于觉得其 contentView 使用哪些子类型,该属性定义如下:

 typedef enum : NSInteger {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;

  为显示的内容按组建立数据模型类,命名为 WJQCarGroup ,声明下列属性:

 //WJQCarGroup.h
@interface WJQCarGroup : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, strong) NSArray *cars; //当前组所有行的数据
@end

  接下来在 ViewController.m 文件中导入 WJQCarGroup.h ,并在类扩展中声明 NSArray 属性用来存放所有组的数据,如下:

 //ViewController.m
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups; //保存所有组的数据
@end

  使用懒加载技术重写 carGroups 属性的 getter 方法,如下:

 //ViewController.m
#pragma mark - 懒加载
- (NSArray *)carGroups {
if (_carGroups == nil) {
WJQCarGroup *cg1 = [[WJQCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"高端大气上档次";
cg1.cars = @[@"奥迪", @"宝马"]; WJQCarGroup *cg2 = [[WJQCarGroup alloc] init];
cg2.title = @"日韩系列";
cg2.desc = @"还不错";
cg2.cars = @[@"本田", @"丰田"]; WJQCarGroup *cg3 = [[WJQCarGroup alloc] init];
cg3.title = @"欧美品牌";
cg3.desc = @"价格昂贵";
cg3.cars = @[@"劳斯莱斯", @"布加迪", @"小米"];
_carGroups = @[cg1, cg2, cg3];
}
return _carGroups;
}

  最后,修改 UITableViewDataSource 协议的相关方法:

 //ViewController.m
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%s", __FUNCTION__);
return self.carGroups.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%s", __FUNCTION__);
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.cars.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s %d %d", __FUNCTION__, indexPath.section, indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
WJQCarGroup *carGroup = self.carGroups[indexPath.section];
NSString *carName = carGroup.cars[indexPath.row];
cell.textLabel.text = carName;
return cell;
} - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.desc;
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
WJQCarGroup *carGroup = self.carGroups[section];
return carGroup.title;
}

参考博客:iOS开发UI篇—UITableview控件简单介绍

实例代码:http://vdisk.weibo.com/s/DiY98QyXCOne0

 

 //ViewController.m
#pragma mark - 控制状态栏是否显示
- (BOOL)prefersStatusBarHidden {
//返回YES代表隐藏状态栏,NO相反
return YES;
}