0

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.

Douglas
  • 2,524
  • 3
  • 29
  • 44

5 Answers5

2

Make the property publicaly accessible.

@property (nonatomic, retain) UIButton *myButton;

Then in cellForRowAtIndexpath

ButtonCell *buttonCell =(ButtonCell *) [tableView dequeueReusableCellWithIdentifier:@"ButtonCell"];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • I think I already did this. Check out my .h file for the ButtonCell class. And I did put your second line in my cellForRowAtIndexPath method. – Douglas Jun 12 '13 at 17:57
  • @Douglas did you include `(ButtonCell *)` to typecast – Lithu T.V Jun 13 '13 at 04:18
  • ., Yes I did, although I am not 100% sure why to do it! Will post some more code later to show. Thanks again for all your time. – Douglas Jun 13 '13 at 11:09
1

Mistake in uppercase maybe ?

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

Instead of :

[ButtonCell.myButton setHidden:NO]; // Trying to access class variable
A-Live
  • 8,904
  • 2
  • 39
  • 74
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
  • I can't use lowercase buttonCell. This was created in cellForRowAtIndexPath, and I want to hide the button in another method. – Douglas Jun 12 '13 at 17:58
1

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.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • How would I do this. I tried to get an instance of the ButtonCell, but it did not work in the method I call reloadData. – Douglas Jun 12 '13 at 18:00
  • You have to know the indexPath of the row you are wanting to update, then it is just a matter of something like this - ButtonCell *buttonCell = [self.myTableView cellForRowAtIndexPath:nowIndex]; – LJ Wilson Jun 12 '13 at 19:01
  • thanks. I will try this tonight and let you know how it goes. – Douglas Jun 12 '13 at 21:17
  • the size of my table view changes so I never know how many rows I will end up with. However, the Button cell is always last. So I was able to substitute your nowIndex, with my last cell. However, I get the error incompatible pointer types initializing 'ButtonCell *_strong' with an expression of type 'UITableViewCell*'. Any ideas? Thanks so much for your help. – Douglas Jun 12 '13 at 22:57
1

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;
A-Live
  • 8,904
  • 2
  • 39
  • 74
bbarnhart
  • 6,620
  • 1
  • 40
  • 60
  • thanks for your time. The cell that has my button is always the last cell. I have figured out how to get this last cell, but am unsure how to use cellForRowAtIndexPath. I followed ElJay's suggestion, but that didn't work, please have a look. Any ideas would be appreciated. – Douglas Jun 12 '13 at 23:15
  • Actually the cell is the last cell in section 3. – Douglas Jun 12 '13 at 23:31
1

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.

Lenrocexe
  • 81
  • 2
  • Check Lithu T.V's answer. What you missed is the cast to ButtonCell. Your myButton instance would just be regarded as a regular UITableViewCell without that cast. Have to comment on my own answer because I do not have enough rep yet. – Lenrocexe Jun 12 '13 at 15:59