Remove the UITableView string by clicking the custom UIButton

Basically, I want to delete a row when I click a button that is part of that row.
I cannot use the commit edit style because I want to unsubscribe and delete using the same button.
enter image description here

When I click the subscribe button, I want to delete this particular line.
so any idea how i can do this.

+1
ios objective-c uitableview
source share
7 answers

It works with the following code

In case of pressing a button

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]; 
+3
source share

but. Add a target and a callback to the button (I suppose you already did this)

b. In the callback function, loop over the supervisors to find the UITableViewCell, and then find its index, for example:

 -(void)deleteButtonPressed:(UIButton*)button { UIView* candidate = button; while (![candidate isKindOfClass:[UITableViewCell class]]) { candidate = candidate.superview; } NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:cell]; ... 

from. Update your model first (very important)

 [self.dataArrayThatFeedsTableView removeObjectAtIndex:indexPathToDelete.item] 

e. Call the delete line

 [self.tableView deleteItemsAtIndexPaths:@[indexPathToDelete]]; 
+2
source share

When you create a string in cellForRowAtIndexPath , assign indexPath.row to the button tag, it is a good idea to add some number to avoid confusion with other buttons, for example:

 cell.deleteButton.tag = 100 + indexPath.row 

when the button is pressed, release the sender to the button, restore the button tag (remember to subtract 100), and you know which line you want to delete.

+1
source share

You can get the position of the pressed button to find the indexPath, and from there find the cell with this button:

 var position: CGPoint = sender.locationInView(self.tableView) var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(position)! var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) 
0
source share

You should get the indexPath button that was clicked. Then delete the selected index paths using a standard API call. For example:

 - (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]; } 
0
source share

Subclass UIButton as CellButton using a custom property

 @property (nonatomic,strong) NSIndexPath *indexPath; 

set the custom class property for your button element as new, and in cellForRowAtIndexPath

 cell.btnSubscribe.indexPath=indexPath; [cell.btnSubscribe addTarget:self action:@selector(subscribe:) forControlEvents:UIControlEventTouchUpInside]; 

now in function

 -(IBAction)subscribe:(id)sender{ CellButton *button=(CellButton *)sender; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:button.indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 

try this answer. i'm sure it will work

0
source share

Pressing a line and deleting it should be possible as follows: In didSelectRowAtIndexPath you can implement the following:

 [myArrayWithSubscriptions removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

If you only want to press UIButton to delete, use this code in the action that runs 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]; 

Thus, you have indexPath from the cell in which its UIButton is involved, and should be able to continue to delete it. I type this on the go and check this code later for any typos.

-one
source share

All Articles