0

I'm trying to add a "favorite button" to a custom cell in a UIableView.

The cell has a label and detailLabel that show fine. I must be doing something wrong but I can't figure out what it is. My code is below, and I changed my original method (commented) after studying this question.

My code is as follows:

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

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
    }
    cell.nameLabel.text = [myMutableArray valueForKey:@"Name"];
    cell.detailLabel.text = [myMutableArray valueForKey:@"Detail"];

    if (isFavorite)
    {
    NSLog(@"fave");
        UIImage *btnImage = [UIImage imageNamed:@"goldStar.png"];
        [cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];

        //[cell.favoriteButton setImage:[UIImage imageNamed:@"yellowStar.png"] forState:UIControlStateNormal];

    } else {
        NSLog(@"out of fave");
        UIImage *btnImage = [UIImage imageNamed:@"greyStar.png"];
        [cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];

        //[cell.favoriteButton setImage:[UIImage imageNamed:@"greyStar.png"] forState:UIControlStateNormal];
    }

    return cell;
}

MyCustomCell.h

@interface MyCustomCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *detailLabel;
@property (nonatomic, strong) IBOutlet UIButton *favoriteButton;

MyCustomCell.m

@implementation MyCustomCell

@synthesize nameLabel = _nameLabel;
@synthesize detailLabel = _detailLabel;
@synthesize favoriteButton = _favoriteButton;

- (void)awakeFromNib {
    _favoriteButton = [[DBTileButton alloc] init];
}

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


}

I've checked and the button is hooked up ok in IB. Thanks ahead for any advice.

UPDATE I notice in the log message that the image frame is set at 0,0,0,0. I'm attaching a shot of the custom cell IB setup. Is something amiss there? enter image description here

Community
  • 1
  • 1
ICL1901
  • 7,632
  • 14
  • 90
  • 138

1 Answers1

1

As far as I know IBOutlet for any sub classes of UIView shouldn't be strong! Anyway you no need to use IBOutlet, if you wanna initialize your button programmatically: at first, you forgot to set frame of your button and add it to cell view via addSubview. But it is easier way: just add button on storyboard, set custom class and remove this:

_favoriteButton = [[DBTileButton alloc] init];

EDIT: set class for your storyboard's button like on picture

enter image description here

Then change your outlet:

@property (nonatomic, weak) IBOutlet DBTileButton *favoriteButton;

And finally don't forget to connect outlet to your button and remove programmatically initializing.

aquarium_moose
  • 355
  • 1
  • 11
  • I tried setting a weak reference - same result. I didn't set the frame as the button is set in the storyboard, and it's set to the correct class. I appreciate your ideas though. +1 – ICL1901 Mar 03 '15 at 19:35
  • when you did this `_favoriteButton = [[DBTileButton alloc] init];` you lost reference on your storyboard's button! This new button, that you did initialize, not even in the cell – aquarium_moose Mar 03 '15 at 19:39
  • sorry, i missed that. I've reset the class methods to this custom button (very cool imho), and logging the button shows this: out of fave button: > – ICL1901 Mar 03 '15 at 19:42
  • and I cut the init method. – ICL1901 Mar 03 '15 at 19:45
  • I found it. Of all things, it was the image. I swapped it for an image that I knew worked, and all is well. Thanks to all. – ICL1901 Mar 03 '15 at 19:55