Delete a UITableView row by click on custom UIButton

醉酒当歌 提交于 2020-01-10 05:53:06

问题


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 delete with same button.

On click on subscribe button i want to delete that particular row.
so any idea, how can i do it.


回答1:


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];



回答2:


a. Add the target and callback to the button (I presume you already did that)

b. In the callback function, loop over the superviews to find the UITableViewCell and then find the index of it, like so:

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

c. Update your model first (very important)

[self.dataArrayThatFeedsTableView removeObjectAtIndex:indexPathToDelete.item]

d. Call the delete row

[self.tableView deleteItemsAtIndexPaths:@[indexPathToDelete]];



回答3:


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.




回答4:


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)



回答5:


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];
   }



回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/29007807/delete-a-uitableview-row-by-click-on-custom-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!