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

Ios 如何隐藏tablview特定单元格上的按钮?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想隐藏UITableView的某些单元格上的按钮,但我无法找到我做错了什么,我的UITableView的某些单元格同时包含文本和按钮,而某些单元格仅包含单行或多行文本。我想在UITableView的某些单元格上显示按钮。我做了以下代码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *celliden

我想隐藏
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;
    }
但这段代码将在第6和第5单元以及第2和第3单元添加按钮,我只需要第6和第5单元的按钮。

我想您应该试试

开始添加按钮,稍后再添加。 像

我想你应该试试

开始添加按钮,稍后再添加。 像


您不应该在
cellforrowatinexpath
中添加按钮作为
[cell addSubView:button]
。dequeReusableCellForIndexPath请求
表视图
获取单元格。
tableView
如果没有或传递了一个旧的,则创建一个。如果你收到一个旧的,那么我可能已经有一个按钮。如果再添加一个按钮,最终将得到一堆按钮

你可以通过两种方式实现你想要的: 您可以在
情节提要
中创建单元格,例如,添加按钮并在
UITableViewCell
的子类中创建一个插座。然后,您可以根据单元格的位置隐藏或取消隐藏按钮

另一种方法是,如果按钮差异太大,您可以在故事板中创建两个具有不同标识符的单元格,例如“cellWithButton”和“cellWithButton”

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格;
if(indexPath.row<5){
cell=[tableView dequeueReusableCellWithIdentifier:@“CellWithOutbatton”];
}否则{
单元格=[tableView dequeueReusableCellWithIdentifier:@“cellWithButton”];
}
//完成其余的配置
返回单元;
}

您不应该在
单元格中添加按钮,因为
[cell addSubView:button]
位于
cellforrowatinexpath
内。dequeReusableCellForIndexPath请求
表视图
获取单元格。
tableView
如果没有或传递了一个旧的,则创建一个。如果你收到一个旧的,那么我可能已经有一个按钮。如果再添加一个按钮,最终将得到一堆按钮

你可以通过两种方式实现你想要的: 您可以在
情节提要
中创建单元格,例如,添加按钮并在
UITableViewCell
的子类中创建一个插座。然后,您可以根据单元格的位置隐藏或取消隐藏按钮

另一种方法是,如果按钮差异太大,您可以在故事板中创建两个具有不同标识符的单元格,例如“cellWithButton”和“cellWithButton”

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格;
if(indexPath.row<5){
cell=[tableView dequeueReusableCellWithIdentifier:@“CellWithOutbatton”];
}否则{
单元格=[tableView dequeueReusableCellWithIdentifier:@“cellWithButton”];
}
//完成其余的配置
返回单元;
}
您只需

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];
}
简单地说,你可以

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];
}
用这个

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;
}
它将消除您的代码和可读性

用这个

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


它将消除您的代码和可读性

您应该在第6和第5个单元格中设置[cell addSubview:button]

您应该在第6和第5个单元格中设置[cell addSubview:button]

步骤1:礼貌一点,确保以格式良好的方式显示代码。使用标记访问按钮,然后使用将“hidden”属性设置为“是”。@WasimSafdar不为this@Simon,她正在给按钮“button.tag=indexPath.row”分配标记。我想,他从@Cristian得到了更好的答案。第1步:礼貌一点,确保以格式良好的方式展示代码。使用标记访问按钮,然后使用“hidden”属性设置为“是”。@WasimSafdar不使用标记this@Simon,她正在为按钮分配标签“button.tag=indexPath.row”“我想,他从@Cristian那里得到了更好的答案。我也厌倦了这个,但它在第2和第3单元格上添加了按钮,也在第6和第5单元格上发布了一条新评论让我试试这个@CristianPena谢谢谢谢@CristianPena你救了我它的工作非常感谢我也厌倦了这个,但它在第2和第3单元格上添加了按钮,也在第6和第5单元格上发布了我的帖子一个新的评论让我试试这个@CristianPena谢谢谢谢@CristianPena谢谢你救了我它的工作奇妙的谢谢不起作用不知道为什么,我昨天做了你的第一个代码,但它会把那个按钮放在对应的或其他单元格上。昨天你试过我的答案了吗?我刚刚发布了我的答案。我的意思是我昨天从不同的地方试过了,和你现在给出的一样,它不起作用。我不知道为什么@user3182143我会说你没有试过回答这里的任何代码。尝试每个答案。他们很快会给出根据您的代码自定义代码。首先,您必须有耐心。您可以轻松地找到解决方案。您可以。不工作此不知道原因,我昨天做了你的第一个代码,但它会把那个按钮放在对应的或其他单元格上。昨天你试过我的答案了吗?我刚刚发布了我的答案。我的意思是我昨天从不同的地方试过了,和你现在给出的一样,它不起作用。我不知道为什么@user3182143我会说你没有试过回答这里的任何代码。尝试每个答案。他们很快会给出答案。根据您的代码自定义代码。首先,您必须有耐心。您可以轻松找到解决方案。您可以。
 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];
  }