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
Related
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];
I want to create a button (not dynamically) bellow the last row in the table. All the rows and sections are fixed. Do I have to create another section and row just to add a button? and if yes how can I ajust the button to the same width and curvature of the cell?
Here is what I did. I create a specific section for the button and one row for it.
}else if(indexPath.section ==2)
{
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellButton setFrame:CGRectMake(0.0f, 5.0f, cell.frame.size.width, 44.0f)]; // not filling the round parts of the cell
[cellButton setBackgroundColor: [UIColor yellowColor]];
[cellButton setTitle:#"test" forState:UIControlStateNormal];
[cell.contentView addSubview:cellButton];
}
return cell;
You don't. Set your button as the table footer view of your table view.
tableView.tableFooterView = myButton;
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);
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.
i am trying to add a button to the tableview and no matter what frame i specify the button is not visible. This is a custom UITableViewCell and I am adding the button in the code rather than in the xib file is so that I can click on the button and perform an action which is not didSelectRowAtIndexPath:
I have the following code in cellForRowAtIndexPath: What am I missing?
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = bframe;
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
try this
UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];