izan
izan

Reputation: 737

A custom Button in a UITableViewCell

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?

Upvotes: 0

Views: 413

Answers (3)

Adarsh V C
Adarsh V C

Reputation: 2314

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

Upvotes: 0

H6_
H6_

Reputation: 32788

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:

enter image description here

and will be places directly in the cell.

Upvotes: 0

Aravindhan
Aravindhan

Reputation: 15628

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];

Upvotes: 1

Related Questions