TableView使用及优化
来源:互联网 发布:hadoop与云计算的关系 编辑:程序博客网 时间:2024/06/06 00:36
#import <UIKit/UIKit.h>
@interface MainViewController : UITableViewController
@end
------------------------------------------------------------------------------------------------------
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
#pragma mark 实例化视图
- (void)loadView
{
self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStylePlain];
}
#pragma mark - 数据源方法
#pragma mark 分数数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
#pragma mark 数据行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (0 == section) {
return 10;
} else if (1 == section) {
return 20;
} else {
return 15;
}
}
#pragma mark 表格行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 表格行每次出现都会被实例化一次,对系统的负担会比较大
// 所谓可重用标示符,是用来在缓冲池查找缓冲单元格使用的字符串
// 1)指定可重用标示符字符串内容
static NSString *CellID = @"MyCell";
// 2) 让tableView去缓冲池查找是否有可重用的表格行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
// 3) 如果没有找到,则使用可充用标示符实例化新的单元格对象
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
NSLog(@"实例化单元格 %@", indexPath);
}
// 设置表格内容
NSString *str = [NSString stringWithFormat:@"itcast-%02d组-%03d行", indexPath.section, indexPath.row];
[cell.textLabel setText:str];
return cell;
}
#pragma mark - 标题行
// 如果是字符串形式,本身不需要进行优化,因为通常会有一个数据字典维护,要显示的字符串,直接通过section从数组中提取即可
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"第%d组", section];
}
#pragma mark 自定义视图标题行
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 使用可重用标示符才能做到表格行的优化
// 要实现标题行的优化,需要实例化:UITableViewHeaderFooterView
// 1) 指定static可重用标示符
static NSString *HeaderID = @"MyHeader";
// 2) 使用可重用标示,让表格在缓冲区查找是否存在可重用的标题行视图
UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderID];
// 3) 如果没有找到缓存的标题行视图,则使用可重用标示符实例化新的表格行视图
if (header == nil) {
// 必须要使用initWithReuseIdentifier方法,实例化UITableViewHeaderFooterView视图
header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:HeaderID];
[header setFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
NSLog(@"实例化标题行 %d", section);
// 自定义标题栏视图内容部分的代码,需要写在 header == nil 分支语句中
// 以下代码实际上在通过代码编写自定义视图时的,视图初始化方法的内容
// 注意:对于自定义控件的代码,不要写在分支语句的外部,否则就到不到优化的效果了
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"hello" forState:UIControlStateNormal];
[button setFrame:header.bounds];
[header addSubview:button];
}
NSLog(@"标题行视图的子视图数量:%d", header.subviews.count);
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
#pragma mark - 页脚明细
@end
- TableView使用及优化
- storyboard使用及tableview示例
- 用泛型来优化 TableView Cells 的使用体验
- TableView优化
- tableview优化
- 关于tableview性能的优化及卡顿现象
- tableView使用SDWebImage加载图片时滚动优化
- TableView使用
- TableView的优化
- TableView 性能优化
- TableView的优化
- 如何优化tableView
- iOS tableView滚动优化
- iOS之tableView优化
- TableView 性能优化
- tableView如何优化
- tableView cell性能优化
- 关于tableview优化
- 布隆过滤器
- 李红霞第二章第11题
- 直接插入排序
- 持续更新-jQuery发微-关于jQuery和JS原生混用的方法
- QT 与 MFC 的区别
- TableView使用及优化
- P51胡晓燕第四题
- machine learning_ANN与BP算法
- UvaOJ 592 Island of Logic
- 生成模型与判别模型
- 文章标题
- 【OpenCV】vector<Mat>
- 浏览器表单向服务器提交中文出现乱码问题
- 李红霞第二章第一题