隐藏UITableViewCell上的按钮

时间:2015-08-05 07:52:25

标签: ios objective-c uitableview

我在每个UIButton上都有两个UITableViewCell,我基本上试图设置两个按钮,以便在按下其中一个按钮时隐藏。我在网上找到的唯一一件事就是用.tag来记录新闻。

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

    // Configure the cell...
    cell.textLabel.text = [self.random objectAtIndex:indexPath.row];
    UIButton * acecept = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    acecept.frame = CGRectMake(cell.bounds.origin.x + 200, cell.bounds.origin.y + 20 , 50, 30);
    [acecept setTitle: @"Yes" forState:UIControlStateNormal];
    [acecept addTarget:self action:@selector(waffles:) forControlEvents:UIControlEventTouchUpInside];
    acecept.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:acecept];

    self.deny = [UIButton buttonWithType:UIButtonTypeRoundedRect];



    _deny.frame = CGRectMake(cell.bounds.origin.x + 250, cell.bounds.origin.y + 20 , 50, 30);
    [_deny setTitle: @"no" forState:UIControlStateNormal];
    [_deny addTarget:self action:@selector(cereal:) forControlEvents:UIControlEventTouchUpInside];
    _deny.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:_deny];

    UIButton * button = (UIButton *) acecept;
    button.tag = button.tag+1;

    /* if (acecept.hidden) {
     [_deny setHidden:YES];
     } */

    return cell;
}

这就是我按下它们时隐藏它们的方式:

-(IBAction) waffles:(id)sender {

    NSLog(@"waffles");


    UIButton * button = (UIButton *) sender;
    button.tag = button.tag+1;

    NSLog(@"%ld", (long)button.tag);

    waff = button.tag;


    [sender setHidden:YES];

    if(sender.hidden == YES){
        [self.deny setHidden:YES];
    }

}


-(IBAction)cereal:(id)sender{

    NSLog(@"cereal");
    [sender setHidden:YES];
}

我尝试了多个if statements并标记了按钮。我想我需要以某种方式获取按下按钮之一的单元格视图的编号,然后将其放在每个IBAction中。

3 个答案:

答案 0 :(得分:0)

.h 中以这种方式声明按钮(带有插座)

@property (weak, nonatomic) IBOutlet UIButton *buttonOne;

@property (weak, nonatomic) IBOutlet UIButton *buttonTwo;

.m:

-(IBAction)presentPrincipalViewController:(id)sender {

   [self.buttonOne setHidden:YES];
   [self.buttonTwo setHidden:YES];
}

如果你想隐藏所有按钮:

-(IBAction)presentPrincipalViewController:(id)sender {

     for (UIButton* someButton in self.view.subviews) {
          [someButton setHidden:YES];
    }
}

答案 1 :(得分:0)

您使用的方法不对。

您需要使用两个按钮创建自定义tableview单元格。

在那个单元格的.m课程中,你可以为这两个按钮编写IBActions。

在这些IBActions中,您可以隐藏这两个按钮。

答案 2 :(得分:0)

我已对您的代码进行了更改..

检查以下代码

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


    static NSString * indentifer = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = [self.random objectAtIndex:indexPath.row];

    UIButton * acecept = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    acecept.frame = CGRectMake(cell.bounds.origin.x + 200, cell.bounds.origin.y + 20 , 50, 30);
    [acecept setTitle: @"Yes" forState:UIControlStateNormal];
    [acecept addTarget:self action:@selector(waffles:) forControlEvents:UIControlEventTouchUpInside];
    acecept.backgroundColor = [UIColor whiteColor];
    acecept.tag = 1;
    [cell.contentView addSubview:acecept];


    UIButton * deny = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    deny.frame = CGRectMake(cell.bounds.origin.x + 250, cell.bounds.origin.y + 20 , 50, 30);
    [deny setTitle: @"no" forState:UIControlStateNormal];
    [deny addTarget:self action:@selector(cereal:) forControlEvents:UIControlEventTouchUpInside];
    deny.backgroundColor = [UIColor whiteColor];
    deny.tag = 2;
    [cell.contentView addSubview:_deny];

    return cell;
}

-(IBAction) waffles:(id)sender {

    NSLog(@"waffles");

    UIButton *aceceptBtn = (UIButton *) sender;
    [aceceptBtn setHidden:YES];

    UIButton *denyBtn = [aceceptBtn.superview viewWithTag:2];
    [denyBtn setHidden:YES];
}


-(IBAction)cereal:(id)sender{

    NSLog(@"cereal");
    UIButton *denyBtn = (UIButton *) sender;
    [denyBtn setHidden:YES];

    UIButton *aceceptBtn = [denyBtn.superview viewWithTag:1];
    [aceceptBtn setHidden:YES];
}