1.UITableView概述
- 在众多移动应用中,我们能够看到各式各样的列表数据,例如QQ消息,微博,网易新闻,微信朋友圈等等,都是列表数据,并且可以滚动
- 在IOS中,苹果为我们提供了UITableView控件,专门用来展示列表数据,UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳,所以在IOS开发中我们一般使用TableView来展示列表数据
2.UITableView基本属性介绍
- (void)viewDidLoad {
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tv];
}
- tableView要想展示数据,必须设置数据源,这里我们设置数据源为控制器,还必须实现数据源的三个方法
- (void)viewDidLoad {
tv.dataSource = self;
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (0 == section) {
return 3;
}else
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *ce11 = [[UITableViewCell alloc] init];
cell.textLabel.text = [NSString stringWithFormat:@"我是数据%zd组,%zd行",indexPath.section,indexPath.row];
return cell;
}
tv.rowHeight = 80;
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
}
tv.sectionHeaderHeight = 100;
tv.sectionFooterHeight = 33;
-(NSString *)tableView:(nonnull UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"表头%zd",section];
}
-(NSString *)tableView:(nonnull UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return [NSString stringWithFormat:@"表尾%zd",section];
}
-(UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return [UIButton buttonWithType:UIButtonTypeContactAdd];
}
-(UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [UIButton buttonWithType:UIButtonTypeInfoLight];
}
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
tv.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
tv.tableHeaderView = [[UISwitch alloc] init];
tv.separatorColor = [UIColor redColor];
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
};
tv.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tv.sectionIndexColor = [UIColor orangeColor];
tv.sectionIndexBackgroundColor = [UIColor greenColor];
-(nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(nonnull UITableView *)tableView{
return @[@"A",@"B",@"C",@"D"];
}