iOS(学习) UITableView
来源:互联网 发布:数据脱敏与数据匿名 编辑:程序博客网 时间:2024/05/19 13:42
UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。
UITableViewStylePlain:
UITableViewStyleGrouped
UITableView中每行数据都是一个UITableViewCell,在这个控件中为了显示更多的信息,iOS已经在其内部设置好了多个子控件以供开发者使用。如果我们查看UITableViewCell的声明文件可以发现在内部有一个UIView控件(contentView,作为其他元素的父控件)、两个UILable控件(textLabel、detailTextLabel)、一个UIImage控件(imageView),分别用于容器、显示内容、详情和图片。使用效果类似于微信
表视图 UITableView 1.UITableView 继承于 UIScrollView 可以滚动 2.为什么需要 UITableView ? 当有很多内容要展示的时候 UIScrollView就不能满足开发需求 , 3.为什么UITableView 可以展示很多内容? 使用类似手扶电梯一样‘重用机制’ 4.UITableView 的每一条数据对应的单元格叫做 call 是UITableViewCell的一个对象。继承UIView UITableView 可以显示分区, 每一个分区,每一个分区称 ‘section’, 每一行row, 编号从0开始。 系统提供了一个专门的类来整合。 section 和 row ,NSIndexPath section 和 row 代表一个 TableViewCall 在 UITableView 上的位置
#import "ViewController.h"#import "HeedTableViewCell.h"#import "DetailTableViewCell.h"#define CellIdentifierHeader @"CellIdentifierHeader"#define CellIdentifierDetail @"CellIdentifierDetail"@interface ViewController () <UITableViewDataSource, UITableViewDelegate>{ NSMutableArray *_dataSource;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // // 判断当前系统是否 是7.0以上的 if ([[[UIDevice currentDevice] systemVersion]floatValue] > 7.0?YES:NO) { // 这个方法是7.0 之后才有的 // 这个方法有什么作用 :解决了导航栏的一些缺陷 self.edgesForExtendedLayout = UIRectEdgeNone; } UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStylePlain]; // 这两个属性 要遵循代理 tableView.dataSource = self; tableView.delegate = self; /** * // 设置分区的高度 想要分区高度统一 就可以这样设置, * // 多个分区 分区高度不一样 用这个方法设置:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; */ // tableView.rowHeight = 103; [self.view addSubview:tableView]; /** // 自定义的 注册表视图 [tableView registerNib:<#(nullable UINib *)#> forCellReuseIdentifier:<#(nonnull NSString *)#>] //系统的 注册表视图 [tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>] */ // 注册表视图 [tableView registerNib:[UINib nibWithNibName:@"HeedTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifierHeader]; [tableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifierDetail]; NSArray *firstArray = @[@"好友动态", @"附近的人", @"兴趣部落"]; NSArray *secondArray = @[@"游戏",@"购物", @"音乐", @"直播", @"附近的群"]; NSArray *imageArray = @[[UIImage imageNamed:@"yx"], [UIImage imageNamed:@"gw"], [UIImage imageNamed:@"yy"], [UIImage imageNamed:@"zb"], [UIImage imageNamed:@"fjq"]]; // 大数组 圈套小数组 _dataSource = [NSMutableArray arrayWithObjects:firstArray, secondArray, imageArray, nil];}#pragma mark ---- UITableViewDataSource, UITableViewDelegate ----- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 判断分区 返回多少行 NSInteger num = 0; switch (section) { case 0: { num = 1; } break; case 1:{ NSArray *secondArray = _dataSource[1]; num = secondArray.count; } default: break; } return num;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (indexPath.section == 0) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierHeader forIndexPath:indexPath]; HeedTableViewCell *headerCell = (HeedTableViewCell *)cell; NSArray *firstArray = _dataSource[0]; headerCell.friendLabel.text = firstArray[0]; headerCell.nearbyLabel.text = firstArray[1]; headerCell.clubLabel.text = firstArray[2]; [headerCell.friendBtn setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal]; [headerCell.nearbyBtn setImage:[UIImage imageNamed:@"fj"] forState:UIControlStateNormal]; [headerCell.clubBut setImage:[UIImage imageNamed:@"xq"] forState:UIControlStateNormal]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierDetail forIndexPath:indexPath]; DetailTableViewCell *detailCell = (DetailTableViewCell *)cell; NSArray *imageArray1 = _dataSource[2]; detailCell.iconImageView.image = imageArray1[indexPath.row]; NSArray *secondArray = _dataSource[1]; detailCell.titleLabel.text = secondArray[indexPath.row]; // 这是系统的 按钮 detailCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }// HeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierHeader forIndexPath:indexPath];// cell.textLabel.text = @"zyx";// cell.friendLabel.text = @"好友动态";// cell.nearbyLabel.text = @"附近的人";// cell.clubLabel.text = @"兴趣部落"; return cell;}// 返回多少个分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2;}// 修改 分区的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height; switch (indexPath.section) { case 0: height = 103; break; case 1: height = 58; default: break; } return height;}- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *string; switch (section) { case 0: string = @"A"; break; case 1: string = @"B"; break; default: break; } return string;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
运行效果:
源码下载:GitHub
0 0
- IOS UITableView 学习笔记
- IOS UITableView学习Demo
- ios学习:UITableView(一)
- IOS学习之UITableView
- IOS学习之 UITableView
- iOS(学习) UITableView
- iOS 学习笔记 uitableView
- IOS学习之UITableView
- iOS开发学习笔记 UITableview
- 转发iOS UITableView学习笔记
- ios学习之UITableView(一)
- IOS UI学习之UITableView
- IOS UITableView 代理方法学习
- iOS开发学习之UITableView
- IOS学习 UITableView 基本属性
- IOS学习 UITableView 委托方法
- iOS学习 UITableView 单选
- iOS学习笔记03-UITableView
- 二叉树 非递归遍历
- Python根据经纬度求两点间距离
- BIT2014级软件学院程序设计-12 编程珠玑
- 属性动画相关使用
- Highcharts技巧总结(持续更新)
- iOS(学习) UITableView
- Android开发中实现多点触摸
- IIS发布asp.net项目,要做的准备工作及添加项目
- java小游戏第三弹 贪吃蛇
- TCP/IP参考模型
- Python脚本语言学习
- PAT 1104. Sum of Number Segments (20)
- C++ STl队列、栈应用
- BIT2014级软件学院程序设计-13 平面上的邮局