A UITextField in a UITableViewCell - UIButton
I have successfully implemented a UITextField in a UITableViewCell. Just like this:
.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
Interface Builder
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];
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
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.
精彩评论