// ViewController.m
// 单元格复用
//
// Created by Beiwo on 16/2/29.
// Copyright © 2016年 你国哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSInteger _newCount;//记录创建次数
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_newCount = 0;
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
tableView.rowHeight = 70;
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*———————————————————————————————核心———————————————————————————————————————————*/
/*
单元格复用:
屏幕上之多能显示N个单元格,那么我们一共需要创建N+1单元格,即可完成表视图的显示任务
优势:节省内存
*/
//1.设置一个单元格重用的标记 identifier 字符串
static NSString *identifier = @"qq_cell";
//2.判断屏幕显示的单元格外 是否有带有标记的cell
//如果存在显示reuse cell 则直接显示
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//如果不存在reuse cell 则创建新cell
if (cell == nil) {
_newCount++;
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
NSLog(@"创建第%ld个单元格",_newCount);
/*———————————————————————————————核心—————————————————————————————————————*/
//单元格的共性
UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds];
label.font = [UIFont boldSystemFontOfSize:30];
label.textColor = [UIColor redColor];
label.tag = 101;
[cell addSubview:label];
}
//内容一定在大括号外面
UILabel *cellLabel = [cell viewWithTag:101];
cellLabel.text = [NSString stringWithFormat:@"%ld %ld",indexPath.section,indexPath.row];
//返回
return cell;
}
@end
Day.02.29 单元格复用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- collectionView的复用无疑是一个很牛X的机制,然而,在实际项目中,我们也常常会被这一机制所困扰!近段时...
- 9Tech·iOS开发交流群:219960444 UITableView继承自UIScrollview,是苹果为我...