0

我正在为我的 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;
}

滚动出现问题。

4

4 回答 4

0

问题是 UITableView 出于性能考虑使这些单元格可重用。

您还必须在 cellForRowAtIndexPath 中设置此条件。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* reusableCellIdentifier = @"";
  if(indexPath.row == 0){
    reusableCellIdentifier = @"firstCell";
  }else{
    reusableCellIdentifier = @"otherCells";
  }
}
于 2013-08-26T11:17:29.893 回答
0
  1. 在 cell.h 文件中定义所有按钮和属性。将您的 cell.m 文件代码替换为

     lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
    lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
    [lObjImageButton setBackgroundColor:[UIColor greenColor]];
    [self addSubview:lObjImageButton];
        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];
    
    
            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];
    

2.在返回单元格之前在您的表格委托方法中添加以下内容;

if(indexPath.row==0){

            cell.lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
            cell.lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
cell.lObjImageButton2.hidden =TRUE;

}
else{
cell.lObjImageButton2.hidden =FALSE;
        cell.lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);

        cell.lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);

            cell.lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);

}

希望它会帮助你。

于 2013-08-26T11:50:28.060 回答
0

使用多态性而不是检查自定义单元格中的条件。

写两个类:

@interface KSRTwoButtonCell : UITableViewCell { ... }

@interface KSRThreeButtonCell : UITableViewCell { ... }

然后做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL wantThreeButtonCell = (indexPath.row == 0);

    NSString *cellIdentifier;
    if (wantThreeButtonCell) {
        cellIdentifier = @"ThreeButtonCell";
    }
    else {
        cellIdentifier = @"TwoButtonCell";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        if (wantThreeButtonCell) {
            cell = [[KSRThreeButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        else {
            cell = [[KSRTwoButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    }

    return cell;
}
于 2013-08-26T11:37:42.653 回答
0

创建- (void)resetCell将单元格重置为初始状态的方法,并创建- (void)configureImageView:(int)rowIndex配置图像视图的方法BSImageCell

- (void)configureImageView:(int)rowIndex
{
    if (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];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    if (cell == nil) {

        cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    else
    {
        [cell resetCell];
    }
    [cell configureImageView:indexPath];

    return cell;
}
于 2013-08-26T13:30:35.927 回答