#import "RootViewController.h"
//第二步: dataSource,遵守协议|| UITableViewDataSource
@interface RootViewController () <UITableViewDataSource>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
// UITableView,表视图, 继承于 UIScrollView, 用于展示具有相同数据格式的内容, 只有一页; (因此有滚动效果, 并且一般数据格式一样)
//一个 UITableView有多个(section), 分区从0开始
//每个分区有多行(row), 行数从0开始 (所以都是从(0, 0)开始的)
//一个 UITableView, 有一个表头, 一个表尾, 和多个分区
//一个分区, 有一个区头, 一个区尾, 和多行
//一行有一个 cell, cell是重复使用的
//参数2,1:plain 没有样式 2:grouped分组样式(默认加上区头, 无标题)
//创建有自己的 init 方法, 一般创建区 API 里面找
UITableView *myTabView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
myTabView.backgroundColor = [UIColor whiteColor];
//行高,默认为100
myTabView.rowHeight = 100;
//分割线的颜色
myTabView.separatorColor = [UIColor redColor];
//分割线的样式
// myTabView.separatorStyle = UITableViewCellSeparatorStyleNone;//;聊天的时候看不见线条;
// 注:UITableViewCellSeparatorStyleSingleLineEtched, 用在分组中
//表头(重在展示)
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 240)];
imageView.image = [UIImage imageNamed:@"1.jpg"];
myTabView.tableHeaderView = imageView;//方法名一般都是可阅读的散文
//表尾(清除虚拟的行数),因为 tabView 是虚拟的
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
// label.text = @"想看下面吗?请付费";
// myTabView.tableFooterView = label;
myTabView.tableFooterView = [[UIView new] autorelease];//清楚了所有虚拟行, new = alloc + init
//第一步: 指定数据源, 提供数据
myTabView.dataSource = self;
//第一步:
//注册单元格类型(为展示做准备), 单元格, UITableViewCell,继承于 UIView, 在 UITableView上展示数据用
[myTabView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"A"];
[self.view addSubview:myTabView];
[myTabView release];//注意: 释放的及时
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//第三步: 实现协议方法
#pragma mark - UITableViewDataSource(有2个方法必须实现)
//分区的个数, 可选择的, 默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}
//必须实现的
//某分区里面有几行,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
}
return 5;
}
//当展示一行数据时, 需要提供视图(UITableViewCell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell, 单元格视图, 继承于 UIView, 在 UITableView上展示数据用
//注: 每次滑动, 都要创建方法; 需要重复使用!!, cell 重用机制, reuseIdentifier,重用标识符
/* 错误的, 用占用大量内存, 内存不够用,看 cpu
NSLog(@"%d, %d", indexPath.section, indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"辉哥真帅"];
cell.imageView.image = [UIImage imageNamed:@"2.jpg"];//图片
cell.textLabel.text = [NSString stringWithFormat:@"只有祖祖在, 世界充满爱(%d, %d)", indexPath.section, indexPath.row];
cell.textLabel.numberOfLines = 0;
// NSIndexPath, 用于储存单元格的位置(分区, 行)
// indexPath.section, 分区
// indexPath.row, 行
return cell;
*/
//第二步:
//去重用池找 cell, 如果没有找到, 会根据你注册的 cell 类型和标识符, 自动创建 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"A" forIndexPath:indexPath];
//cell 里面有很多的方法,属性, 有图片, label...
cell.imageView.image = [UIImage imageNamed:@"2.jpg"];//图片添加到图片视图上
cell.textLabel.text = [NSString stringWithFormat:@"只有祖祖在, 世界充满爱(%d, %d)", indexPath.section, indexPath.row];//有参数的文本, 一般用 NSString stringWithFormat()..
cell.textLabel.numberOfLines = 0;
//选中样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;//默认为灰色. 聊天一般会去除颜色
NSLog(@"%d, %d", indexPath.section, indexPath.row);
return cell;
}
//分区的区头标题,区头区尾, 有悬浮效果;不写, 默认不展示
//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// return [NSString stringWithFormat:@"分区%d的区头", section];
//}
/*
//分区的区尾标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return [NSString stringWithFormat:@"分区的%d 的区尾", section];
}
*/
@end
iOS 编程中 UI 部分UITableView基础知识
最新推荐文章于 2022-01-05 13:22:56 发布