我正在为我的 tableview 单元格做一个自定义单元格,我在 customcell 类中编写一个条件,如果 indexpath.row=0 为单元格创建 2 个图像,否则创建 3 个图像,
我的自定义单元类
if (appDelegate.rowIndex==0)
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1, nil];
}
else
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
UIButton *lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
[lObjImageButton2 setBackgroundColor:[UIColor blueColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton2];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
}
这里 appdelegate.rowindex 值在我的视图控制器内部 cellforrowindex 中设置为 appdelegate.rowstauts=indexpath.row
当我运行该应用程序时,该项目按我的意愿执行,第一个单元格 2 个图像和剩余的单元格 3 个图像,但是当我滚动时条件不匹配并使第 6 个单元格成为 2 个图像和总和 3 个单元格作为 2 个图像和我的第一个单元格变成 3 个图像..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
appDelegate.rowIndex=indexPath.row;
NSLog(@"cell.index= %i",cell.index);
if (cell == nil) {
cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
滚动出现问题。