K.Honda
K.Honda

Reputation: 3106

A UITextField in a UITableViewCell - UIButton

I have successfully implemented a UITextField in a UITableViewCell. Just like this:

enter image description here .

I did the above using code in: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

Now I would like to place a UIButton underneath these 2 UITableViewCells. Can I code a UIButton in? How can I do this and where?

Thanks.

New Button Position

enter image description here

Interface Builder

enter image description here

Upvotes: 0

Views: 851

Answers (3)

Rahul Vyas
Rahul Vyas

Reputation: 28720

You can use this to add a button to tableView's footer View

tableView.tableFooterView = [[[UIButton alloc] initWith:] autorelease];

or you can add a button to section footer view. using the method

viewForFooterInSection

Upvotes: 1

KingofBliss
KingofBliss

Reputation: 15115

You can do it in the same method also,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(indexPath.row==2)
  {
   //Create a UIButton
   //Assign target
   //Add the button as a subview to cell.contentView
  }
else
  {
   //Add the textFields
  }
}

To create a button code as,

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the cell and set the size of the button
    [myButton setTitle:@"Login" forState:UIControlStateNormal];
    // add targets and actions
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // add to a view
    [cell.contentView addSubview:myButton];

Upvotes: 1

StuStirling
StuStirling

Reputation: 16191

You would probably have to make a UIViewController the controller of the table view. That way you can add a UIButton onto the view as well as the table view.

Just make the UIViewController a UITableViewDelegate and UITableViewDataSource, create a UITableView in the UIViewController and copy the code from the UITableViewController into the UIVIewController. Then add a UIButton below it.

This is what I have done in the past. I only use UITableViewController if I want just a table view dislayed for things like lists.

Upvotes: 0

Related Questions