一:关于UITableView
- 在iOS的应用中最为常见,例如:短信,电话,微信等
- 继承自UIScrollView,拥有它的所有特性
- 跟大部分控件一样,是UIKit在UIKit库中 001
- 只分行,不分列,易操作(扩展:可以分行分列的是UICollectionView,功能更强大,加下来会讲,到时候会一起来说最常用的为什么是tableView,用兴趣的可以自己提前看一下)002
- 具有强大功能并且使用方便,封装完善,最早的一批空间,而collection是6.0
二:基本介绍
- UITableView有两种风格:UITableViewStylePlain、UITableViewStyleGrouped
- UITableView中每行数据都是一个UITableViewCell
- 头视图、尾视图,组头视图、组尾视图 003
- 代理delegate,数据源代理dataSource,004
UITableView的代理.png
![909891-e8084cdcb2f3a7f5.png](https://i-blog.csdnimg.cn/blog_migrate/947db475df6e4ddd88903a9541774fd0.webp?x-image-process=image/format,png)
UITableViewDataSource.png
![909891-a7bdb84b9d7f8224.png](https://i-blog.csdnimg.cn/blog_migrate/eaa57d82f3235a41b8198bf74268370a.webp?x-image-process=image/format,png)
UITableViewDelegate.png
5.tableView为了节省内存,使用复用机制,复用的机制
三:UITableViewCell基本介绍
- 四种样式
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
};
- 一些复杂的页面可以进行自定义样式
注:代码实现
//
// PBViewController.m
// CBSS_iOS
//
// Created by mass on 2017/10/18.
// Copyright © 2017年 Bin_Peng. All rights reserved.
//
#import "PBViewController.h"
@interface PBViewController ()<UITableViewDataSource,UITableViewDelegate>//签订代理协议
@end
@implementation PBViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建tableView
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
//设置代理对象
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
//实现代理
#pragma mark -<UITableViewDataSource,UITableViewDelegate>-
#pragma mark -数据源代理
//共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
//每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
//每行的具体展示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identify = @"cellIdengtify";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld组%ld行",indexPath.section,indexPath.row];
return cell;
}
#pragma mark -操作代理
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50.0f;//不设置,默认44.0f;
}
//组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
//组尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 30.0f;
}
//组头视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *identify = @"headerIdengtify";
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identify];
if (!headerView) {
headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identify];
}
headerView.textLabel.text = [NSString stringWithFormat:@"我是第%ld组头",section];
return headerView;
}
//组尾视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
NSString *identify = @"footerIdengtify";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identify];
if (!footerView) {
footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identify];
}
footerView.textLabel.text = [NSString stringWithFormat:@"我是第%ld组尾",section];
return footerView;
}
@end
![909891-63374edfa8c6d3ac.png](https://i-blog.csdnimg.cn/blog_migrate/9cdee11975674a10d630cfac5e79f5d2.webp?x-image-process=image/format,png)
程序截图.png