R. Dewi
R. Dewi

Reputation: 4271

how to hide a button programmatically

I have to create a button programmatically, and set an image background at that button. There is one condition, I want to hide that button and show a UITableViewCellAccessoryDetailDisclosureButton, but I don't know how.

The button is still visible and the UITableViewCellAccessoryDetailDisclosureButton falls on it.

Here is my code:

if (filemodels.fileType == @"project" && filemodels.fileExpanse == @"none") {
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    NSLog(@"open expanse");
    //[displayBut isHidden];
    //displayBut.hidden = YES;
    //[displayBut setImage:nil forState:UIControlStateNormal];
    displayBut.alpha = 0.0;

}

Can anyone help?

Update:

Regarding Vladimir's answer. I have changed it as per his suggestion, but the button is still not hiding.

The code is like this:

if ([filemodels.fileType isEqualToString:@"project"] && [filemodels.fileExpanse isEqualToString:@"none"]) {

     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
     [cell.contentView addSubview:nil];
     [displayBut setHidden:YES];
     [displayBut setBackgroundImage:nil forState:UIControlStateNormal];

}

Upvotes: 2

Views: 1735

Answers (2)

R. Dewi
R. Dewi

Reputation: 4271

[Merged update/question as answer into OP]

Upvotes: 1

Vladimir
Vladimir

Reputation: 170849

The 1st problem with your code is if-condition - you compare not the string values, but pointer values and so don't get right result. The correct way to compare strings in your case is to use -isEqualToString: method:

if ([filemodels.fileType isEqualToString:@"project"] && [filemodels.fileExpanse isEqualToString:@"none"]){
...

Then if you previously set your cell's accessoryView to be a displayBtn then you may need to set it to nil to make cell use accessoryType instead of your custom view - just making it hidden may be insufficient here.

Upvotes: 2

Related Questions