gpt4 book ai didi

ios - 为什么 customTableViewCell 不同且速度慢?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:36 29 4
gpt4 key购买 nike

在 Storyboard 中,我设置了 tableviewcell 标识符“firstCell”和类“FirstTableViewCell”。
在 UIViewcontroller 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"firstCell";
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
NSLog(@"%@",cell);
if (cell == nil)
{
cell = (FirstTableViewCell *)[[FirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
return cell;
}

输出:

2013-09-12 20:30:10.378 InfoDrop[4120:c07] FirstTableViewCell: 0x75624b0; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = CALayer : 0x7562610    

2013-09-12 20:30:10.584 InfoDrop[4120:c07] FirstTableViewCell: 0x75624b0; baseClass = UITableViewCell; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = CALayer: 0x7562610

2013-09-12 20:30:10.586 InfoDrop[4120:c07] <FirstTableViewCell: 0x8181a40; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x81a46e0>>

2013-09-12 21:18:08.042 InfoDrop[4415:c07] <FirstTableViewCell: 0x83993e0; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x8398c90>>

为什么不一样?你可以看到第二个单元格和第一个单元格之间的时间比第三个单元格和第二个单元格之间的时间要大,为什么?谢谢。

@implementation FirstTableViewCell

在单元格中:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)setCa:(Categories *)ca{

UILabel *nameLabelView = (UILabel *)[self.contentView viewWithTag:1];
nameLabelView.text=ca.name;

UILabel *detailLabelView = (UILabel *)[self.contentView viewWithTag:2];
detailLabelView.text=[ca.count stringValue];

UIView *left =(UIView *) [self.contentView viewWithTag:3];
UIView *fill;



int tmp = [ca.id intValue];

switch (tmp) {
case 1:
fill = [[CreditView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];

break;
case 2:
fill = [[BankView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
break;
case 3:
fill = [[QuestionView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
break;
case 4:
fill = [[SoftwareView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
break;
case 5:
fill = [[NoteView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
break;

default:
break;
}

[left addSubview:fill];
[left setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];

}

-(void)setcaNil:(int) sum {


UILabel *nameLabelView = (UILabel *)[self.contentView viewWithTag:1];
nameLabelView.text=@"All";

UILabel *detailLabelView = (UILabel *)[self.contentView viewWithTag:2];
detailLabelView.text=[[NSNumber numberWithInt:sum] stringValue];

UIView *f =(UIView *) [self.contentView viewWithTag:3];

AllView * ui = [[AllView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
[f addSubview:ui];


[f setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];
}

最佳答案

我强烈建议您在 FirstTableViewCell 类中实现 prepareForReuse 方法。在该方法中,清除单元格不能重复使用的所有元素(示例:UILabel 文本值)。我不建议用这种方法重新创建 UIView,因为性能可能会受到影响。

但是如果不查看与该问题相关的执行栈中的全部代码,很难给出完整的判断。

更新根据您的代码更新,我建议您执行以下操作

//In your .h cell file

@property(nonatomic,strong)UILabel *nameLabelView
@property(nonatomic,strong)UILabel *detailLabelView
@property(nonatomic,strong)UIView *leftView

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//Here initialize your uilabels and uiview using CGRectZero as frame
self.leftView = [UIImageView alloc] initWithFrame:CGRectMake(0,0.0,27.0,27.0)];
[self.contentView addSubview:self.leftView];
}
return self;

-(void)prepareForReuse
{
//clean up the text values for your labels
}

-(void)setCa:(Categories *)ca{
UILabel *nameLabelView = (UILabel *)[self.contentView viewWithTag:1];
nameLabelView.text=ca.name;

UILabel *detailLabelView = (UILabel *)[self.contentView viewWithTag:2];
detailLabelView.text=[ca.count stringValue];
UIView *fill;
switch (tmp) {
case 1:
fill = [[CreditView alloc]init];
break;
case 2:
fill = [[BankView alloc]init];
break;
case 3:
fill = [[QuestionView alloc]init];
break;
case 4:
fill = [[SoftwareView alloc]init];
break;
case 5:
fill = [[NoteView alloc]init];
break;
default:
break;
}
[self.lefView addSubview:fill];
//BTW this code has a lot of potential for a good refactoring
}

-(void)layoutSubViews{
self.leftView.frame = CGRectMake(0.0,0.0,27.0,27.0);
//Do the rest of positioning for your elements here
}

对于这种情况调用:

[left setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];

是矫枉过正,因为您强制系统重绘您的 View 。取而代之的是在 layoutSubviews 中进行定位,每次单元格出现在表格中时,uiTableViewController 都会调用它。

关于ios - 为什么 customTableViewCell 不同且速度慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764962/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com