How to add a button without using a custom cell in a UITableView?

How to add a custom button on UITableViewCelland then delete a cell using this button without using Interface Builder and Custom Cell?

+2
source share
2 answers

If you really want to add a custom button WITHOUT a subclass, just add the button to the contentView of the cell:

[cell.contentView addSubview:customButton];

You can set all the characteristics of the button: frame, target, selector, etc. Ad then used the above call to add it to the cell.

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=//whatever
[customButton setImage:anImage forState:UIControlStateNormal];
[customButton setImage:anotherImage forState:UIControlStateHighlighted];
[customButton addTarget:self action:@selector(delete) forControlEvents: UIControlEventTouchUpInside];
//yadda, yadda, .....

You can tag it as well.

customButton.tag = 99999;

So you can find it later:

UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];

, , , , , ... .

+8

, , UITableViewDataSource, - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath. :

    - (BOOL)tableView:(UITableView *)tableView 
canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
   return YES; 
}

:

 - (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
 forRowAtIndexPath:(NSIndexPath *)indexPath 
{
   // Database removal code goes here...
}

, UITableViewController UITableViewDataSource, - :

MyClass : UITableViewController <UITableViewDataSource>

, viewController self.

+1

All Articles