做项目会遇到一个场景,提前定制好的cell,布局好图片,文字等等,但是突然某一行需要加一个单独的按钮或者其他控件,如果cell列表过多,这时候滑动过于频繁,会出现显示错乱,怎么解决?
第一,构造好cell后removeFromSuperview掉新增元素
YFFukaTurnLabelLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFFukaTurnLabelLabelCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
for (UIView* vi in cell.contentView.subviews) {
if ([vi isEqual:lineImg]){
[vi removeFromSuperview];
}
else if ([vi isEqual:headerLabel]) {
[vi removeFromSuperview];
}
}
第二 找到哪个特别的组中的特殊的行 单独加入,并且不用重复加入
else if(indexPath.row==3){
cell.titleLabel.text = @"支付方式:";
// cell.msgLabel.text = @"银行卡支付";
if (_dataModel.payMode == 3||_dataModel.payMode == 1) {
cell.msgLabel.text = @"货到付款";
}else if (_dataModel.payMode == 5){
cell.msgLabel.text = @"快捷支付";
}else if(_dataModel.payMode == 2){
cell.msgLabel.text = @"汇款支付";
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
if(!headerLabel){
headerLabel=[[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth-150, 0, 120, 40)];
headerLabel.font=FONT(14);
headerLabel.textAlignment=NSTextAlignmentRight;
headerLabel.text=@"查看收款账户";
headerLabel.textColor=[UIColor blueColor];
headerLabel.backgroundColor=CLEARCOLOR;
}
//这个需要放在外面
[cell.contentView addSubview:headerLabel];
}
}
这里的添加 [cell.contentView addSubview:headerLabel]; 需要放在外面才行
控件的创建也可以在didload里面创建,不用判断非空,cell里面创建需要判断nil在alloc,
其他复用的情况,利用设置初始值然后重写就可以了,prepareForReuse
@implementation YFFukaTurnLabelLabelCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)prepareForReuse
{
[super prepareForReuse];
self.msgLabel.textColor=nil;
self.rightInstance.constant=18;
}