Basically I want to delete row on click event of a button which is a part of that row.
I can not use commit editing style because i want to perform unsubscribe and dele
You can get the position of the button clicked in order to find the indexPath and from there find the cell that has that button:
var position: CGPoint = sender.locationInView(self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(position)!
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
When you create row in cellForRowAtIndexPath
assign an indexPath.row to the button's tag, good idea is to add some number to avoid confusion with others button, for example:
cell.deleteButton.tag = 100 + indexPath.row
when the button is clicked cast the sender to the button, restore the button's tag (remember to subtract 100) and you know which row you want to remove.
It works with following code
On button click event
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.table];
NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonPosition];
[self.arraylist removeObjectAtIndex:indexPath.row];
[self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Make a sub class of UIButton as CellButton with taking custom property like this
@property (nonatomic,strong) NSIndexPath *indexPath;
set the custom class property for your button as the new one created and in cellForRowAtIndexPath
cell.btnSubscribe.indexPath=indexPath;
[cell.btnSubscribe addTarget:self action:@selector(subscribe:) forControlEvents:UIControlEventTouchUpInside];
now in the function
-(IBAction)subscribe:(id)sender{
CellButton *button=(CellButton *)sender;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:button.indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
give a try to this answer. am sure this will work
Tapping a row and deleting it should be possible in a way like this:
In your didSelectRowAtIndexPath
you can implement the following:
[myArrayWithSubscriptions removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
If you want to only tap the UIButton to delete use this code in the action behing the UIButton.
CGPoint point;
NSIndexPath *indexPath;
point = [sender.center locationInView:tableView];
indexPath = [tableView indexPathForRowAtPoint:point];
[myArrayWithSubscriptions removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
That way you have the indexPath from the cell that had its UIButton tapped, and should be able to continue to delete it. I'm typing this on the go and will check this code later for any typos.
You should get the indexPath
of the button that was tapped. And then delete the selected index paths using standard API call. Like so:
- (IBAction) didTapSubscribeButton:(UIButton*)sender {
UIView* candidate = button;
while (![candidate isKindOfClass:[UITableViewCell class]]) {
candidate = candidate.superview;
}
NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:candidate];
[self.dataSourceArray removeObjectAtIndex:indexPath.row] //Considering that this is a single data source table view.
NSArray *indexPaths = [NSArray alloc]initWithObjects:@[indexPath],nil];
[self.tableView beginUpdates];
tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}