A custom Button in a UITableViewCell - iphone

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
}
}

Related

I am using same action for 5 buttons and want to know which button is called?

I am using same action for 5 buttons and want to know which button is called
In such a case please try assigning unique tags to the buttons.
In the target method regain the button tag as follows
Eg
-(void)targetMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedButtonTag = button.tag ;
}
Give tag value to your button in nib then add this in your button action:
allbtn = sender;
btntag = allbtn.tag;
NSLog(#"btntag:%d",btntag);
if(btntag==1)
{
}
Simple now you easily find what button you tap.
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);
button.tag=1;
[view addSubview:button];
-(void)aMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedBtnTag = button.tag ;
Nslog("clicked button tag is %d",clickedBtnTag);
}
Try this and then please revert me..
use:
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);
button.tag=1;
[view addSubview:button];
then use:
-(void)aMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedBtnTag = button.tag ;
Nslog("clicked button tag is %d",clickedBtnTag);
}
Try this and then please revert me..

Add UIButton programmatically from a IBAction

I am trying to make an action where every time you press a button, it adds a button to the view. Nothing I have tried works.
It would be much appreciated if someone could clear up what to do.
This is the most recent attempt at my button-adding button:
-(IBAction) addButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self
action:#selector(buttonTouched:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Try the above codes.
you need to add this
#import <QuartzCore/CoreAnimation.h>
then Do the follow this
-(IBAction)buttonTouched:(id)sender{
NSLog(#"New Button Clicked");
}
-(IBAction)addButton:(id)sender
{
UIButton *btnallstates;
UIImage *statesbtnimg=[UIImage imageNamed:#"1.png"];
btnallstates=[[UIButton buttonWithType:UIButtonTypeCustom]retain];
btnallstates.tag=1;
btnallstates.frame=CGRectMake(103, 127, 193, 31);
[btnallstates.layer setBorderWidth:0];
[btnallstates addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[btnallstates setBackgroundImage:statesbtnimg forState:UIControlStateNormal];
[self.view addSubview:btnallstates];
}
Your code is correct. Only change this line
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to show the button on the view.
UIButtonTypeCustom does not show up although it is added to the view.
Your can understand this by changing the color of the button.
e.g change the code to this.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor redColor];
Hope it works. Enjoy.
Try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

Putting a button in table footer view

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

how to create various UIButtons with a for case in iPhone SDK?

I have something like this...
for(int i=0;i<10;i++){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button1" forState:UIControlStateNormal];
button.frame = CGRectMake(00.0, 00.0, 100.0, 30.0);
[view addSubview:button];
}
It's possible to instantiate each button with his own id? For example button+i ?
Thanks in advance!
You can set a numeric id to each button using the tag property:
button.tag = i;
You can then get the button instance afterwards with the code
[view viewWithTag:i];
provided you have added it to a view, like using [view addSubview:button].

section header in uitableview - reusidentifier?

I have a table with customized headers for the sections. In addition to that, I implemented a collapsing feature, so that when a header is touched, all non-adressed sections are reduced, and the adressed section is expanded.
I realized that by using UIButtons as background views. Furthermore, the buttons have different colors and text, depending on their state (expanded vs not expanded).
I have a problem, similar to that reusidentifier problem, i.e. if u dont reuse cells already allocated in a table, certain fragments appear, if u start to scroll. Here it only happens to my first section, nevertheless it appears fragmented and duplicated....
Is there anything similar to reusing headerviews already allocated once like the method reusIdentifier for UITableViewCells?
Or is there a better approach?
Allocating the Buttons in advance to store them in array didnt do the trick for me...
Here's some code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
[button setShowsTouchWhenHighlighted:NO];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(collapse:) forControlEvents:UIControlEventTouchUpInside];
button.tag = section;
[button setAdjustsImageWhenHighlighted:NO];
button.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:154/255.0f blue:201/255.0f alpha:1.0f];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
if (section == 0) {
// [button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[button setTitle:#" Newsroom" forState:UIControlStateNormal];
} else {
[button setTitle:[NSString stringWithFormat:#" Sektion %d", section] forState:UIControlStateNormal];
}
if (section == mySection) {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor whiteColor]];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor colorWithRed:96.0f/255.0f green:96/255.0f blue:97/255.0f alpha:1.0f]];
}
return [button autorelease];
}
I also implemented a collapsing section table using UIButton header views. What I found was that it was folly trying to cache and resuse the views, because you don't know when the UITableView is done with them (I got crashes).
I recommend implementing your state change by: updating your data model; performing all the row insert/deletes to perform the collapses/expand; then doing a [tableView reloadData], which will call viewForHeaderInSection, where you use the state of your data model to return the appropriately formatted uibutton view.