如何隐藏tablview特定单元格上的按钮?

时间:2016-07-15 09:53:16

标签: ios objective-c uitableview

我想在UITableView的某个单元格上隐藏按钮,但我无法找到我做错了什么,我的UITableView的某些单元格同时包含文本和按钮,而某些单元格只有单行或多行的文本。我想在UITableView的某个单元格上显示按钮。我做了这段代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSString *cellidentifier= @"cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];

    UIButton *  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(5, 2, 100, 30);
    [button setTitle:@"World" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor= [UIColor clearColor];
    button.tag = indexPath.row;
    [cell addSubview:button];

    if(indexPath.row ==0)
    {


        cell.textLabel.attributedText = [dataArray objectAtIndex:0];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
     if (indexPath.row == 1)
    {


    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dataArray objectAtIndex:1]] placeholderImage:nil];
    cell.imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
    }
    if(indexPath.row ==2)
    {


        cell.textLabel.attributedText = [dataArray objectAtIndex:2];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    if(indexPath.row ==3)
    {

        cell.textLabel.attributedText = [dataArray objectAtIndex:3];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    if(indexPath.row ==4)
    {

        cell.textLabel.attributedText = [dataArray objectAtIndex:4];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 0;


    }
      if( indexPath.row == 5)
     {

        cell.textLabel.attributedText =[dataArray objectAtIndex:5];
        cell.textLabel.textAlignment = NSTextAlignmentRight;
    }


      if(indexPath.row == 6)

     {
        //[button setHidden:YES];
         cell.textLabel.attributedText = [dataArray objectAtIndex:6];
         cell.textLabel.textAlignment = NSTextAlignmentCenter;
         cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
         cell.textLabel.numberOfLines = 0;

    }
    if ( indexPath.row == 6 || indexPath.row == 5)
    {
        [button setHidden:NO];
    }
    else {
        [button setHidden:YES];
    }
      return cell;
    }

但是这个代码也会在第2和第3个单元格的第2和第3个单元格上添加按钮,我只需按钮第6和第5个单元格。

6 个答案:

答案 0 :(得分:4)

您不应在[cell addSubView: button]内添加cellForRowAtIndexPath按钮。 dequeReusableCellForIndexPath向tableView询问单元格。 tableView创建一个,如果它没有任何或通过旧的。如果你收到旧的,那么我可能已经有了一个按钮。如果你添加另一个,你最终会得到一堆按钮。

您可以通过两种方式实现您想要的目标: 您可以在storyboard中创建单元格,例如,添加按钮并在UITableViewCell的子类中创建出口。然后,您可以根据单元格隐藏或取消隐藏按钮。

另一方面,如果按钮太不相同,您可以在故事板中使用不同的标识符创建两个单元格,例如“cellWithButton”和“cellWithoutButton”。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell;

    if (indexPath.row < 5) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithoutButton"];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithButton"];
    }

    // Do the rest of your configuration

    return cell;
}

答案 1 :(得分:0)

我认为你应该尝试

在开始时添加按钮,稍后再添加。 等,

if ( indexPath.row == 6 || indexPath.row == 5)
{
   [cell addSubview:button];
}
else {
   [button removeFromSuperview];
}

答案 2 :(得分:0)

只需你可以做到

UIButton *  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];// or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30);
[button setTitle:@"World" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.textColor = [UIColor blueColor];
button.backgroundColor= [UIColor clearColor];
button.tag = indexPath.row;
[cell.contentView addSubview:button];

if(indexPath.row==0)
{
     button.hidden = YES;
}
else if(indexPath.row==1){
     button.hidden = YES;
}
else if(indexPath.row==2){
     button.hidden = YES;
}
else if(indexPath.row==3){
     button.hidden = YES;
}
else if(indexPath.row==4){
     button.hidden = YES;
}
else if(indexPath.row==5 || indexPath.row==6){
     button.hidden = NO;
}

或者

UIButton *  button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30);
[button setTitle:@"World" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.textColor = [UIColor blueColor];
button.backgroundColor= [UIColor clearColor];
button.tag = indexPath.row;

if(indexPath.row==0)
{

}
else if(indexPath.row==1){

}
else if(indexPath.row==2){

}
else if(indexPath.row==3){

}
else if(indexPath.row==4){

}
else if(indexPath.row==5 || indexPath.row==6){
    [cell.contentView addSubview:button];
}

答案 3 :(得分:0)

使用此

UIButton *  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];// or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30);
[button setTitle:@"World" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.textColor = [UIColor blueColor];
button.backgroundColor= [UIColor clearColor];
button.tag = indexPath.row;
[cell.contentView addSubview:button];

if(indexPath.row==5 || indexPath.row==6){
     button.hidden = NO;
}
else {
     button.hidden = YES;
}

它将消除您的代码并且也是可读的。

答案 4 :(得分:0)

 UIButton *  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button.frame = CGRectMake(5, 2, 100, 30);
  [button setTitle:@"World" forState:UIControlStateNormal];
  [button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
  button.backgroundColor= [UIColor clearColor];
  button.tag = indexPath.row;
  //add here 
  button.hidden==YES;
  [cell addSubview:button];

  and after write this condition 
  if ( indexPath.row == 6 || indexPath.row == 5)
   {
  [button setHidden:NO];
  }

答案 5 :(得分:0)

你应该在第6和第5个单元格中制作[cell addSubview:button]