【问题标题】:Custom TableView Cell ,check condition自定义 TableView 单元格,检查条件
【发布时间】:2013-08-28 20:31:17
【问题描述】:

我正在为我的 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 值在我的 viewcontroller 内部 cellforrowindex 中设置为 appdelegate.rowstauts=indexpath.row

当我运行应用程序时,项目按我的意愿执行,第一个单元格 2 个图像,其余单元格 3 个图像, 但是当我滚动时,条件不匹配,使第 6 个单元格成为 2 个图像,将第 3 个单元格作为 2 个图像,而我的第 1 个单元格成为 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;
}

问题在于滚动。

【问题讨论】:

  • 因为当一个重复使用的单元格进入cellForRowAtIndexPath时你什么都不做
  • 只要有可重复使用的单元格,只需将其重置(将其重置为初始状态,然后选择天气以提供两张或三张图像。

标签: iphone ios objective-c ipad ios6


【解决方案1】:

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

你也必须在 cellForRowAtIndexPath 中设置这个条件。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* reusableCellIdentifier = @"";
  if(indexPath.row == 0){
    reusableCellIdentifier = @"firstCell";
  }else{
    reusableCellIdentifier = @"otherCells";
  }
}

【讨论】:

  • 你要我创建 2 个自定义单元格吗?
  • 没有像你那样的自定义单元格......但是你有包含这些自定义单元格的表格视图......你必须在 tableView 中将你的第一个单元格标识为唯一的,以免再次重复使用它
  • 这个功能已经在你的项目中某处......你只需要添加条件
  • Hy -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString* reusableCellIdentifier = @""; if(indexPath.row == 0){ reusableCellIdentifier = @"firstCell"; }else{ reusableCellIdentifier = @"otherCells"; } } 为我工作......解决了!!!!!!
  • 不客气 :D ... 请选择我的答案作为正确答案
【解决方案2】:

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

写两个类:

@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;
}

【讨论】:

    【解决方案3】:
    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);
    
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

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

      - (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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        相关资源
        最近更新 更多