I try to create a button inside the tableview cell without using Interface Builder.
I found this code that I thought it is for creating a button:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
}
but when I try to run the program nothing show up in the simulator.
am I missing something to create the button with this code?
You need to add the button to the cell - have you tried
[cell.contentView addSubview:button]
?
Adjust your button's frame to position it within the contentView and to size the button correctly.
You should write your all code in cellforRow At index Datasource method and for add your button on cell you can use this line of code
[cell.contentView addsubview:buttonobject];
button.frame = CGRectMake(10,10,30,30);
And also change Tour button background or style for your conformation it will be there or not.
It will surly work if its working inform us.
Your Welcome.
You have to add code in this method but first of all add custom cell to your table view,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
forwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[forwordBtn setTitle:#"Forward" forState:UIControlStateNormal];
[forwordBtn addTarget:self
action:#selector(forwordButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
forwordBtn.frame = CGRectMake(0, 0, 10, 10);
[cell.contentview addSubview:forwordBtn];
}
Related
I have implemented createCellForTableview: method in AppDelegate which creates the cell add appropriate UIButton as accessoryView to the cell and returns the cell to the controller.In my viewController i am creating the instance of the accessoryView and adding the target to the button as fallows:
button = (UIButton *)cell.accessoryView;
[button setTitle:#"access" forState:UIControlStateNormal];
[button setTitle:#"access" forState:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(accessButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
My problem here is, the button action not being called.Please help me in resolving this issue.
Thanks in advance
Change the #selector to accessButtonTapped:
why you are passing the even in the selector method ..try this one..
button = (UIButton *)cell.accessoryView;
[button setTitle:#"access" forState:UIControlStateNormal];
[button setTitle:#"access" forState:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(accessButtonTapped:)forControlEvents:UIControlEventTouchUpInside];
may this will help you
If it is accessory button,you need to call this method :
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
I am trying to add button into my table cell like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UIButton* buttonCheckbox = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCheckbox.frame = CGRectMake(0, 177, 56, 26);
buttonCheckbox.backgroundColor = [UIColor redColor];
[buttonCheckbox addTarget:self action:#selector(actionFirst1) forControlEvents:UIControlEventTouchUpInside];
[buttonCheckbox setTitle:#"MyTitle" forState:UIControlStateNormal];
[cell addSubview:buttonCheckbox];
but actionFirst1 event is not fired. if i add the button [self.view addSubview:buttonCheckbox] instead of [cell addSubview:buttonCheckbox] it works fine. why?
thanks...
You should add this button to the contentView property of your cell
[cell.contentView addSubview:buttonCheckbox];
In fact, all your custom elements inside a UITableViewCell should be placed inside the contentView as this cell elements (contentView, accessorView and image) are all placed on top of any elements that your cell have.
Check this doc for more info on cell about custom UITableviewCell
Hi try below code it may help you out:
if ([indexPath row] == 0)
{
UIButton *myButton = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width,
heigth)];
[cell addSubview:myButton];
cell.accessoryView = myButton;
[myButton addTarget:self action:#selector (actionFirst1)
forControlEvents:UIControlEventTouchUpInside];
}
This code will help you out. Please set frames according to your cell of TableView.
Please see whether it would suffice to add your button as the accessoryView of the table cell. In that case, the iOS framework will position the button in the right place for your cell style itself (though you still need to give it a size). Here's an example from one of my projects:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 100, 27); // only size matters
[myButton setTitle:#"Connect" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(connect:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = myButton;
That's it. You just create the control, make sure it has a size, and set it to the accessoryView.
I am creating a custom button and putting it in my table's footer view but the button is going way out from the right corner.
What is wrong here!?!
Here is my code and output image:
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
I added the button in the UIView and then set the tableFooterView to this UIView object and it worked. We cannot directly put UIButton as tableFooterView.
Try doing like this. Set IBOutlet UIView *footerView; and synthesize it. Add it as a subview using the method-
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Click Me" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
//set action of the button
[button addTarget:self action:#selector(clickPressed:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
One important addition to Legolas' fine answer: You'll need to implement the tableView delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
foot 50;
}
This was driving me nuts because the button (or, in my case, buttons) were not responding to the touch event. Adding the delegate method made it all better. :-)
I have a UITableView with three section. I have a custom UITableViewCell. I want to put a button in the first section of my table view to handle an action.
How to can do this please?
just create the button programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
You can also put the button directly into a UITableViewCell which is located in your first section. In your cellForRowAtIndexPath you can use for example:
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:#"Click me" forState:UIControlStateNormal];
[yourButton setFrame: CGRectMake( 20.0f, 3.0f, 280.0f, 33.0f)];
[yourButton addTarget:self action:#selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:yourButton];
This example will look like this:
and will be places directly in the cell.
If you want to put it in the section header (rather than a cell), you could:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section==0)
{
// create button and return it
}
}
I'd like one of my table rows to be a button that takes up an entire row of my UITableView. I figured the best way to go about this is to instantiate a UIButton, and give it the same frame size as an instance of UITableViewCell, and add that as a subview to the cell. I'm almost there, but quite a few pixels off to not get that perfect touch to it. Is there a better approach to this, or perhapsps can my placement accuracy be fixed up to get that perfect alignment?
cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)];
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
You can fake it in your didSelectRowAtIndexPath: method.
Have the tableView cell act as the button.
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
[self doStuff];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
Use setSelected: animated: to fake a button press and have it fade.
Might do the trick for you.
Class(UITableViewCell) has contentView property.
So I put basic button in contentView of UITableViewCell variable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
・・・
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setTitle:#"OK" forState:UIControlStateSelected];
[btn addTarget:self action:#selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
・・・
return cell;
}
Sorry, poor my English.
if you want it to fit perfectly, just call the cell's bounds as your CGRect. do it like this:
button.frame = cell.bounds;
That should get your button to be exactly the same as your cell. Hope that helps!
Cord
This may be beside the point, but your code may have the problem where another button is added to the cell every time the cell is re-used.
I had the same problem as you. And Ryan gave you the answer: use the cell itself as a button with the didSelectedRowAtIndexPath: method.
You can also add add a UIImage to your UITableViewCell to make the cell appear to be a button.
You can also create a view with a button and place it in the footer of the section above it. That way you don't need to worry about the cell image in the background. I've done this to place two half-width buttons side-by-side in a grouped table view.
FWIW, I put the button in my app at CGRectMake(9, 0, 302, 45)
and I think it looks perfect.
UIButton *btnView = [[UIButton alloc] init];
btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f);
[btnView setImage:[UIImage imageNamed:#"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:#selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];