Reputation: 1073
I'm using a UIButton inside my UITableViewCell, this button can be selected, gold star, and unselected, gray star, as shown bellow.
The problem is that when ever the cell is selected, no matter if the button is selected or not it will turn to a gray color (this gray color is different than the gray color of unselected mode of the star). I've been trying to figure out why this happens, but had no luck.
here is the implementation of the cells,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//set the background of the cells
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"cellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellselected2.png"]];
// set the button content
[button setImage:[UIImage imageNamed:@"star.png" ] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"starSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 10, 24, 24)];
[cell addSubview:button];
return cell;
}
also when I tap the button when the cell is selected the button will totally disappear!
Thanks,
Upvotes: 2
Views: 2596
Reputation: 11
You can certainly set the image for the highlighted state. Another option, if you want to use the same image for UIControlStateNormal
and UIControlStateHighlighted
you can also say
button.adjustsImageWhenHighlighted = NO
, the default is YES
.
Upvotes: 1
Reputation: 1073
I figured what the problem is. The problem is that when the cell is selected the UIButton in the cell will go to it's "highlighted state". If there is no image assigned to be used for the button highlighted state of the button it will look the same as it look in my case.
So I just fixed it by adding the image I'd like to be used on the UIButton highlighted state,
[button setImage:[UIImage imageNamed:@"starSelected.png"] forState:UIControlStateHighlighted];
Hope this helps other people who face the same issue :)
Upvotes: 5
Reputation: 7573
You are adding the button to the cell frame instead of it's contentView.
UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views. Whilest a UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.
replace
[cell addSubview:favButton];
with
[cell.contentView addSubview:favButton];
And that should solve it.
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
It's all in the AppleDocs ^^
Upvotes: 1