Douglas
Douglas

Reputation: 2524

Hide UIButton in Custom UITableViewCell

In my tableview, I have several different custom cells. In one of them, it has a button. This button brings up another view controller. However, It is not needed until the tableview has fully loaded. In cellForRowAtIndexPath I set up all my different custom cells. I can uncomment [buttonCell.myButton setHidden:YES]; and it will hide my button. See below.

else if (indexPath.section == 3)
{
    ButtonCell *buttonCell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell"];

    //[buttonCell.myButton setHidden:YES];
    cell = buttonCell;
}
    return cell;

However, I want to then unhide the button after the tableview loads. I finish loading all my arrays in another method where I call reloadData. In that method, I tried to unhide the button by doing this..

[ButtonCell.myButton setHidden:NO];

But the compiler gives me a warning that property myButton is not found in ButtonCell. Does anyone have any ideas how to go about unhiding my button. What am I doing wrong, and what do I not get! Thanks for all your help.

EDIT 1

My button cell class is... .h #import

@interface ButtonCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)YDI:(id)sender;


@end

.m

#import "ButtonCell.h"
#import "AnotherWebViewController.h"

@implementation ButtonCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (IBAction)YDI:(id)sender
{

}

@end

EDIT 2

With everyone's help that answered (thank you all) I have gotten a bit further, but the button is not showing itself. So I still hide the button in cellForRowAtIndexPath, that works as should. Then in my method that I reload the data in I put the following code.

NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:3];
ButtonCell *buttonCell = (ButtonCell *) [self.tableView cellForRowAtIndexPath:index];
[buttonCell.myButton setHidden:NO];

The ButtonCell with the button is always the fourth section (counting the first as 0) and it only has one row. Any other help would be appreciated. Almost there!

EDIT 3 Got it! However, it was due to a comment that I was able to figure it out. Thanks to @A-Live. Although I do know how to get the cell in a method outside of cellForRowAtIndexPath thanks to ElJay. So I am giving him the check since I learned something new which is why we post questions anyway. So inside my method cellForRowAtIndexPath is where I hide/show the button. I have a BOOL in my App called finished, it is originally set to true. When the table view ends loading it is set to false. So I just used this bool to show/hide the button.

else if (indexPath.section == 3)
{
    ButtonCell *buttonCell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell"];

    if (!_finished)
    {
        [buttonCell.myButton setHidden:YES];
    }else{
        [buttonCell.myButton setHidden:NO];
    }

    cell = buttonCell;
}
    return cell;

Once again this is only part of my cellForRowAtIndexPath method. Thanks once again for all the help. I was surprised to see so many answers! Thanks.

Upvotes: 0

Views: 1691

Answers (5)

Lithu T.V
Lithu T.V

Reputation: 20021

Make the property publicaly accessible.

@property (nonatomic, retain) UIButton *myButton;

Then in cellForRowAtIndexpath

ButtonCell *buttonCell =(ButtonCell *) [tableView dequeueReusableCellWithIdentifier:@"ButtonCell"];

Upvotes: 2

Lenrocexe
Lenrocexe

Reputation: 81

Do you have a public accessor for that property in the header file of ButtonCell? Something like @property (nonatomic, retain) UIButton *myButton;
This is how I usually see such a compiler warning.

Upvotes: 1

DCMaxxx
DCMaxxx

Reputation: 2574

Mistake in uppercase maybe ?

[buttonCell.myButton setHidden:NO]; // Trying to access instance variable

Instead of :

[ButtonCell.myButton setHidden:NO]; // Trying to access class variable

Upvotes: 1

bbarnhart
bbarnhart

Reputation: 6710

In your code

[ButtonCell.myButton setHidden:NO];

You are trying to use the object class name instead of the object name. You need to get the cell that contains your button

buttonCell = [tableView cellForRowAtIndexPath:indexPath];
buttonCell.myButton.hidden = NO;

Upvotes: 1

LJ Wilson
LJ Wilson

Reputation: 14427

myButton belongs to a cell. You will need to get an instance of that UITableViewCell and then you can unhide it, this assumes you want to modify the cell's objects outside of cellForRowAtIndexPsth or willDisplayCell.

Upvotes: 1

Related Questions