2

This question is bit related to my earlier question

I am newbie for iPhone application and trying to learn UITableView with JSON. I followed this video for learning.

I created same example using Stoaryboard and also added new screen where I can add data. Now I was trying to delete the data. So what I did is thought is instead of below method, I will add button on each row and on clicking of that I will delete the data.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

But I am not getting how I will get the row details and how to delete.

Any help/ suggestion would be grateful.

Below is how my screen looks like.

enter image description here

Note:

The UITableViewCell is of CUSTOM type.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

2

Update the data model according to edit actions delete using bellow code.

This bellow code is just an example for hot to delete or remove the object from array and also from table .. for more info check the tutorial which i post the link bellow..

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

     if (editingStyle == UITableViewCellEditingStyleDelete) {

         [arryData removeObjectAtIndex:indexPath.row];

        [tblSimpleTable reloadData];

     } 
}

or also using this bellow logic with custom button of cell...

- (IBAction)deleteCustomCellWithUIButton:(id)sender
{
  NSIndexPath *indexPath = [yourTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
  NSUInteger row = [indexPath row];
  [yourTableView removeObjectAtIndex:row];
  [yourTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
}

For more info refer these tutorial...

  1. iphone-sdk-tutorial-add-delete-reorder-UITableView-row

  2. multiple-row-selection-and-editing-in

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • thanks Paras.. I shall check and get back to you incase of any queries. – Fahim Parkar Jan 09 '13 at 12:57
  • I am pulling data from live database. So I believe this will not work. What I believe is, when I click on row 0, I should get id of that row so that I can use that Id to delete the data. Also for this, I would need to set tag while adding the delete button. Correct me if I am wrong... – Fahim Parkar Jan 09 '13 at 13:02
  • @FahimParkar hey here no need for create some special code for that , here roland say is also right here with this example you can get idea that how can you add or delete raw at runtime but also you can delete data from live also .. just put some logic here and you can got it dude.. – Paras Joshi Jan 09 '13 at 13:09
  • @FahimParkar here you need to just remove data from web same as you delete data from array and also you can implement the logic with tag if you use custom button for delete raw.. also lookup in http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-add-delete-data-from-tableview-in-iphone/ this link hope this helpful to you dude.. :) – Paras Joshi Jan 09 '13 at 13:12
  • @FahimParkar i update my answer for how to delete raw and data from custom button tap event dude... :) – Paras Joshi Jan 09 '13 at 13:29
  • how can I take integer value from `[[news objectAtIndex:indexPath.row] objectForKey:@"id"]`? – Fahim Parkar Jan 09 '13 at 13:31
  • [[[news objectAtIndex:indexPath.row] objectForKey:@"id"] intValue]; try this if not get then post comment – Paras Joshi Jan 09 '13 at 13:32
  • hey can you take a look at my [new question](http://stackoverflow.com/questions/14301523/language-switcher-in-ios6) – Fahim Parkar Jan 13 '13 at 07:37
1

You can add a button on each row and set the button.tag to the indexPath.row in the cellForRowAtIndex: method. Then in your button method you can delete the entry from the Array that is used to populate the UITableView and then reloadData

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
0

You have many way to get cell or indexPath of cell:

  • (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
  • (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
  • (NSArray *)indexPathsForRowsInRect:(CGRect)rect
  • (NSArray *)visibleCells
  • (NSArray *)indexPathsForVisibleRows

If you want to handel IBAction of UIButton than use indexPathForRowAtPoint like this :

-(IBAction)myAction:(id)sender
{

CGPoint location            = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath      = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipeCell  = [self.tableView cellForRowAtIndexPath:indexPath];

NSLog(@"Selected row: %d", indexPath.row);
//......

}

OR indexPathForCell

- (void)buttonPressedAction:(id)sender
{
   UIButton *button = (UIButton *)sender;
   // Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
   (UITableViewCell*)cell = [[button superview] superview];
   int row = [myTable indexPathForCell:cell].row;
}

I have taken code from : Detecting which UIButton was pressed in a UITableView

Custom UITableViewCell button action?

Community
  • 1
  • 1
CRDave
  • 9,279
  • 5
  • 41
  • 59