【问题标题】:UITableViewCells with UIButton overlaps while scrolling滚动时带有 UIButton 的 UITableViewCells 重叠
【发布时间】:2014-05-16 18:36:32
【问题描述】:

我需要创建一个表格视图,其中包含一些按钮作为其元素,视图可以正常加载,但是向下滚动时按钮会重叠。我认为旧按钮没有被清除,新按钮被放置在它上面。请帮我解决这个问题。提前致谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath           (NSIndexPath *)indexPath
{
NSLog(@"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 160.0, 25);
    [cell.contentView addSubview:button];


return cell;
}

【问题讨论】:

  • 可以删除 if(cell==nil) 代码块,dequeueReusableCellWithIdentifier 已经够用了。

标签: ios objective-c uitableview uibutton


【解决方案1】:

只需替换行

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

现在

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

  (or) //use this in cell for rowatindexpath

for(UIView *view in cell.contentView.subviews){
    if ([view isKindOfClass:[UIView class]]) {
        [view removeFromSuperview];
    }
}

【讨论】:

    【解决方案2】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath           (NSIndexPath *)indexPath
    {
    NSLog(@"index path %ld",(long)indexPath.row);
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //if(cell == nil)
    //{
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
     //}
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 160.0, 25);
    [cell.contentView addSubview:button];
    
    
    return cell;
    }
    

    【讨论】:

      【解决方案3】:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          NSLog(@"index path %ld",(long)indexPath.row);
      
          NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
          cell = nil;
          if(cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
      
      
      
              UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
              [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
              button.frame = CGRectMake(0, 0, 160.0, 25);
              [cell.contentView addSubview:button];
          }
      
          return cell;
      }
      

      【讨论】:

        【解决方案4】:

        如果你使用 Xib 你可以使用

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
        
        if (cell==nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        

        【讨论】:

          【解决方案5】:

          我建议为此使用标签。请参阅下面的代码。

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath           (NSIndexPath *)indexPath
          {
          
              NSLog(@"index path %ld",(long)indexPath.row);
              static NSString *CellIdentifier = @"Cell";
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          
              if(cell == nil)
              {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
              }
          
              NSInteger buttonTag = 100500;
              UIButton *button = [cell.contentView viewWithTag: buttonTag]
              if(button == nil)
              {
                  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                  button.frame = CGRectMake(0, 0, 160.0, 25);
                  [cell.contentView addSubview:button];
              }
              [button setTitle:[NSString stringWithFormat:@"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
          
              return cell;
          
          }
          

          【讨论】:

            【解决方案6】:

            请在将按钮添加为子视图之前插入以下代码,它会在添加新按钮之前删除单元格内容视图中的所有按钮。

            for(UIView *view in cell.contentView.subviews){
                if ([view isKindOfClass:[UIButton class]]) {
                    [view removeFromSuperview];
                }
            }
            

            【讨论】:

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