问题
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.

Note:
The UITableViewCell
is of CUSTOM type.
回答1:
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...
iphone-sdk-tutorial-add-delete-reorder-UITableView-row
multiple-row-selection-and-editing-in
回答2:
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
回答3:
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?
来源:https://stackoverflow.com/questions/14235433/how-to-find-delete-the-custom-row-detail-of-uitableviewcell