如何删除uitableviewcell中的重复按钮?

时间:2014-10-11 05:41:33

标签: ios

我是新手。我创建了一个UITableView,并且我使用下面给出的代码在特定行(例如,在第二行)中添加了UIButton。当我滚动表格视图时,我得到了表格中的按钮副本。

先谢谢,请给我一个解决方案。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSString *text = [items objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 44.0f);

    if (indexPath.row ==1) {
    return height + (CELL_CONTENT_MARGIN * 2) + CELL_HEIGHT;
    }
    else{
        return height + (CELL_CONTENT_MARGIN * 2);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UILabel *label = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setMinimumFontSize:FONT_SIZE];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label setTag:1];


      //  [[label layer] setBorderWidth:2.0f];





    [[cell contentView] addSubview:label];
    }


      if (indexPath.row==1) {

            UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            //Button.frame = CGRectMake(120, 120, 80, 24);
           NSString *text =@"BUTTON" ;
          CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

          CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


          Button.frame = CGRectMake(65, 150, 160, 24);

          [Button addTarget:self
                       action:@selector(load)
             forControlEvents:UIControlEventTouchUpInside];
            Button.backgroundColor = [UIColor redColor];
           Button.layer.cornerRadius = 5;
            [Button setTitle:text forState:UIControlStateNormal];
               [cell addSubview:Button];
        }
    NSString *text = [items objectAtIndex:[indexPath row]];

   CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    if (!label)
        label = (UILabel*)[cell viewWithTag:1];

    [label setText:text];
    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    return cell;

}

1 个答案:

答案 0 :(得分:2)

获取多个按钮的原因是每次调用cellForRowAtIndexPath时都要在单元格中添加一个按钮实例。此调用经常发生,并且单元格被重用(出列)。

处理自定义单元格的正确方法是子类化单元格,然后在请求正确索引时设置适当的单元格:

if (index == 1) {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"**Cell**"];
else {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"**OtherCell**"];
}