how do i add a custom button on a UITableViewCell ANd Then delete The Cell With That Button Without Using Interface Builder And Custom Cell? the data is loaded from the database..
plz someone help me
how do i add a custom button on a UITableViewCell ANd Then delete The Cell With That Button Without Using Interface Builder And Custom Cell? the data is loaded from the database..
plz someone help me
If the button's sole purpose is to offer deletion you should look into UITableViewDataSource
which has a method called - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
. Implement it like so:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
And then implement:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Database removal code goes here...
}
To use these methods, let your UITableViewController
implement the UITableViewDataSource
protocol by doing something like:
MyClass : UITableViewController <UITableViewDataSource>
in your header file, and be sure to remember to set the viewController's datasource to self
.
If you really want to add a custom button WITHOUT subclassing, 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];
You will need to decide WHEN to add the button, maybe on cell selection, maybe in editing mode... just put the code in the delegate method of your choice.