views:

1941

answers:

2

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

+1  A: 

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.

tmadsen
what about button i want to do all this stuff when i click on a custom button and the button must be created without using interface builder and custom cell.
Rahul Vyas
the canEditRowAtIndexPath method will automatically add a delete button on each row when in edit mode. Try it out :)
tmadsen
but i want to do it with a custom button.
Rahul Vyas
tell me how do i add a custom button without using interface builder and custom cell.then how do i delete the row directly and also the data from the database?
Rahul Vyas
I'm not sure what you mean by custom cell. But I guess you could create a button for each row in the method that returns the cell for each row, and do a [cell addSubview:yourButtonInstance]. Be sure to set the target and action of that button appropriately. Action could be something like -(void)deleteRow:(id)sender where sender would be a pointer to the cell in which the user has clicked the delete button. But I don't get why you would want to do it that way, but that's none of my business of course.
tmadsen
yes you are right.what i want to do is as u says create a button on the default UITableViewCell i'm not creating a customcell and also not using interface builder.i have added a button in place of accessary view.also set the target and action.but now i want that when i click on that button the row should be deleted and also the data in the database deleted.tell me another way of adding a button and layout the button.and also deletion from the database.i am waiting for your answer sir
Rahul Vyas
Go search Google, I'm sure you'll find a stackoverflow question telling you how to add a button to a cell in a table view (you initWithFrame), and the action method should include the deletion routine (RTFM), and after deletion do [(UITableView *)[self view]reloadData].
tmadsen
as u suggested i have added a button and row is also deleted but when i scrolls the table down the rows/cells reappears automatically
Rahul Vyas
Hi, Rahul I too have same problem I created custom button. The button will be in every cell. I need to delete the row by touching the delete button of the cell. Did, you work it out. If so, plz say me how to [email protected]
srikanth rongali
+6  A: 

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.

Corey Floyd