UIButton gets covered when UITableViewCell is selected - ios

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,

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 :)

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 ^^
Ref: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html

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.

Related

what is diffrent between [cell addSubview:button] and [cell.contentview addsubview:button]

i have button in tableviewcell.
what is diffrent between [cell addSubview:button] and [cell.contentview addsubview:button] ?
because when i use [cell addSubview:button] in tableview , button function work fine but my interface in table view messed up and button go to left cell after i scroll in views and back to tableview.
in other hand when i use [cell.contentview addsubview:button] button function not work and nothing save to myarray.
button code:
UIButton *button=(UIButton *) [cell viewWithTag:103];//fav
[button setImage:[UIImage imageNamed:#"unfav.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateSelected];
button.frame = CGRectMake(0,0, 50, 50);
button.tag = indexPath.row;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button]; // add the button to the cell
[cell bringSubviewToFront:button];
The contentView is the default view for the cell in which all the subviews are added.The cell.view is the main view in which contentView is itself added and cell.contentView covers the whole area of the cell.view. But in your case when you added the button to cell.view.
From apple docs about ContentView,
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.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instp/UITableViewCell/contentView

UITableViewCell imageView overlaps text in cell

I have a cell that has some text in it and was trying to add an image to the cell.
This is what Ive tried:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:5.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:#"test2.jpeg"]];
[cell.contentView addSubview:imageView];
cell.imageView.clipsToBounds = YES;
This is what it looks like right now:
Is there a way to avoid the image from overlapping the text in the cell? Thanks in advance!
tableviewcells have their own imageView that will appear to the left of the content or atleast to the left of the cells text.
You are creating an extra imageView that just happens to have the same name.
Just change the location
cell.imageView.center = CGPointMake(x,y);
Check out https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW11 which has a nice example of how to use the built-in UITableViewCell image view.
Every UITableViewCell already has an UIImageView property 'imageView', but it's only shown if you set the imageView properties image property (cell.imageView.image = /* image code */;)
The best thing you can do is create a custom UITableViewCell. Or perhaps add another UILabel to the current cell and set your text in that instead of using the default UILabel.

Get rid of padding for UITableViewCell custom accessoryView

I have a table view cell with a custom accessoryView, but it seems that the way it's laid out by default creates a 15-point margin on the right side. I want to get rid of it.
I can achieve the desired effect by overriding layoutSubviews, but that breaks edit mode animation (the accessory no longer slides in/out).
Any better way to do this?
Add subview instead of accessoryView
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[deleteBtn setBackgroundImage:[UIImage imageNamed:#"trash.png"]
forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor clearColor];
//deleteBtn.alpha = 0.5;
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:#selector(numberDeleteConfirm:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteBtn];

custom UITableViewCell with image for highlighting

I have a custom UITableViewCell (instantiated from XIB) with an image as background. Now I want another image to be the background when the cell is selected (similar to the blue flashing for standard cells). I already tried setting it using setSelectedBackgroundView in (void)setSelected:(BOOL)selected animated:(BOOL)animated or even in didSelectRowAtIndexPath but the background for highlighting wont show up. What am I doing wrong?
In your Custom Cell.xib
Next make this
And Finally do this
have you tried like below one
Call below Method From the TableView DataSource Method (cellForAtIndexPath) For Doing the same Task
- (void)setCellBGColorNormalAndSelectedForTableViewCell:(UITableViewCell*)cell cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[normalCellBG setImage:[UIImage imageNamed:#"box_nonselected.png"]];//Set Image for Normal
[normalCellBG setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:normalCellBG];
UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[selecetedCellBG setImage:[UIImage imageNamed:#"box_selected.png"]];//Set Name for selected Cell
[selecetedCellBG setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selecetedCellBG ];
}
You can set selected background view using the XIB you created for CustomCell . You just need to take UIImageView on to the that XIB (imageview must have same height as that of CustomCell) . Now connect the outlet naming "selectedBackgroundView" for that CustomCell to the UIImageView you have taken as a selected background view . Also check whether the selectionStyle for that CustomCell is not none .

How to force button to stay at tableView cell's bottom?

I add a button as a subview to my tableviewcells. Works fine. But I can't seem to figure out, how to force it to always be at the bottom of the cell. (On top of the separator line if you will). The cell height varies, so a fixed position is not smart. But I have no clue how to add it with a dynamic position. Here's the code for adding the button:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"+1" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 70.0f, 160.0f, 15.0f);
[cell addSubview:button];
Set the autoresizing mask when adding the button. I think the setting you want is:
button.autoresizingMask = UIViewAutoResizingFlexibleTopMargin;
Do this after adding the button to the cell.
You can always check the height of the cell and put the button's frame like this.
button.frame=CGRectMake(0,cellHeight-15,160,15);

Resources