重用机制可以节省很多内存,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath {
static NSString *CMainCell = @" CMainCell "; // 0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell]; // 1
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CMainCell] autorelease]; // 2
}
// Config your cell
cell.textlabel.text = @" XXX "; // 3
return cell;
static NSString *CMainCell = @" CMainCell "; // 0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell]; // 1
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CMainCell] autorelease]; // 2
}
// Config your cell
cell.textlabel.text = @" XXX "; // 3
return cell;
}
可以这样理解,cell有一个地方(假设称为队列),专门存放那些生成过的,但是后来由于滚动tableView而隐藏起来的cell,而代码中语句1,
就是从队列中根据标示符取出一个暂时不用的cell,只有cell为nil,也就是队列中没有旧的cell的时候,才会执行语句,生成一个新的cell。如果有旧的,就不用执行语句2了,这样节省资源,算作一种重用吧。在tableView初始化的时候队列中肯定没有cell的,所以每个cell生成的时候都会执行一遍2,当屏幕显示满了,向上滚动显示下一行时,就会把第一行隐藏,放到那个队列中,然后新增加的一行执行语句1的时候,结果就不是nil了,然后,就跳过语句2了,这样就节约资源了
实现方式,
举个例子来说,在系统刚启动时,tableview可以显示多少个cell,在这里我们假定为10个,在刚开始的时候tableview会生成10个tableviewcell,并且对应有自己的tag值
,假定为0-9。采用下面的方法来实现:在tableview向上滚动的时候,tag为0的cell将不再显示;然后我们把tag为0的cell移动到tag为9的cell下面,重新设置相关的属性,然后将tag为1的cell移动到tag为0的cell下面……依此类推。这也就是所谓的“可重用”。
但是此时被移动的tag为0的cell的一些属性还是保持不变的(包括之前添加的subView
),因此就会出现重叠。所以苹果建议尽量用cell自带的视图(leftview,rightview……)少用自定义。重用也可以被干掉
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell != nil) {
[cell release]; //怎么样?? 换了位置的Cell嚣张不了了吧....
}
这样可以不重用,但内存问题,呵呵
如果想在cell上面加一个Lable,最好的顺序是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath {
static NSString *CMainCell =
@"
CMainCell
";
//
0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
//
1
if (cell == nil) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CMainCell] autorelease];
//
2
UILable *lab=[[UILable alloc]init];
}
// Config your cell
// Config your cell
cell.textlabel.text =
@"
XXX
";
//
3
lab.text=@“”;
return
cell;
}
这样做是为了每次创建cell的时候创建Lable,重用cell时不会再创建Lable造成重叠,并且,每个cell不管是否是重用的,text都会重新设置,Lable的text放在3之后,保证了Lable.text的更新。如果在if里面就设置了Lable的text,滚动时就会造成文字重叠。
关于语句0,如果每个cell的结构完全相同,那没问题,就用这一个标识符,但是如果各个cell结构不完全相同,有的有textfield,有的有button,有的有switch,那就不能互相重用了,只能每行用不同的标识符了,可以方便的利用(@"CMainCell%d", indexPath.row),保证不会重复,多组的再加上组号。那这还有必要用语句2吗?还是需要的,因为当自己滚动隐藏,下次再显示出来的时候,还是可以重用的。标识符完全可以每次用一个新的。
总之,注意添加控件的位置,注意设置控件的位置,注意cell标识符。
当我们的uitableview为透明或者判断cell是否为空时,会发现uitableveiwcell出现重叠,下面为自己的解决办法,提供给各位参考
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
)indexPath{UITableViewCell *cell= nil;
static NSString *reuse= @" cell " ;
if (cell== nil) {
cell= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
} else {
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; // 删除并进行重新分配
}
}
cell.textLabel.text= @" cell " ;
return cell;
}
另一种写法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *cellIdetify = @" cell " ;
UITableViewCell *tvCell = [tableView dequeueReusableCellWithIdentifier:cellIdetify];
if (tvCell == nil)
{
tvCell
=
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify] autorelease];
}
else
{
NSArray
*views =
[tvCell subviews];for (UIView *obj in views) {
if (obj.tag== 1000 || obj.tag== 2000 ) { //只删除指定的画面,不要全部删除,否则tableview的分割线也会被删除
NSLog( @" cell 要删除的子画面是:%@ " ,[obj class ]);
[obj removeFromSuperview];
}
}