今天犯了一个很愚蠢的错误,喷血中...
开始的时候代码是这么写的,注意separateView、progressLabel和customProgressView的位置。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * string = @"string";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
_MyTableView.separatorColor = UITableViewCellSeparatorStyleNone;
}
//separateView
UIView * separateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 20)];
separateView.backgroundColor = RGBACOLOR(247, 247, 247, 1);
[cell.contentView addSubview:separateView];
//progressLabel
UILabel * progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 79, 39, 21)];
progressLabel.text = @"进度";
progressLabel.textAlignment = NSTextAlignmentLeft;
progressLabel.font = [UIFont systemFontOfSize:15];
progressLabel.textColor = LABELTEXTCOLOR;
[cell.contentView addSubview:progressLabel];
//customProgressView
CustomProgressView * customProgressView = [[CustomProgressView alloc]init];
customProgressView.frame = CGRectMake(70, 45, SCREENWIDTH - 90, 90);
customProgressView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:customProgressView];
_customProgressView = customProgressView;
//任务已经完成
if([_stateStr isEqualToString:@"show"])
{
_customProgressView.userInteractionEnabled = NO;
}
return cell;
}
我的customProgressView是一个自定义的带有滑块的的进度条,每次该cell滑出屏幕再滑进屏幕时,我的真机屏幕上就会多出一个customProgressView,画面彻底混乱了...
然后责怪单元格的复用机制没有起到作用...(汗啊...)
其实因为没有理解单元格的复用机制,代码放错了位置,正确的做法是把separateView、progressLabel和customProgressView的代码放在if(cell == nil){} 内。如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * string = @"string";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
_MyTableView.separatorColor = UITableViewCellSeparatorStyleNone;
//separateView
UIView * separateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 20)];
separateView.backgroundColor = RGBACOLOR(247, 247, 247, 1);
[cell.contentView addSubview:separateView];
//progressLabel
UILabel * progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 79, 39, 21)];
progressLabel.text = @"进度";
progressLabel.textAlignment = NSTextAlignmentLeft;
progressLabel.font = [UIFont systemFontOfSize:15];
progressLabel.textColor = LABELTEXTCOLOR;
[cell.contentView addSubview:progressLabel];
//customProgressView
CustomProgressView * customProgressView = [[CustomProgressView alloc]init];
customProgressView.frame = CGRectMake(70, 45, SCREENWIDTH - 90, 90);
customProgressView.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:customProgressView];
_customProgressView = customProgressView;
}
//任务已经完成
if([_stateStr isEqualToString:@"show"])
{
_customProgressView.userInteractionEnabled = NO;
}
return cell;
}
出错的图片稍后补上...