Ios 通过单击自定义UIView按钮删除UITableView行

Ios 通过单击自定义UIView按钮删除UITableView行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,基本上,我想在按钮的点击事件中删除该行,该按钮是该行的一部分。 我无法使用提交编辑样式,因为我想使用同一按钮执行取消订阅和删除操作。 单击“订阅”按钮,我想删除该特定行。 你可以点击按钮的位置来找到indexPath,并从中找到有该按钮的单元格: var position: CGPoint = sender.locationInView(self.tableView) var indexPath: NSIndexPath = self.tableView.indexPathForRowAtP

基本上,我想在按钮的点击事件中删除该行,该按钮是该行的一部分。
我无法使用提交编辑样式,因为我想使用同一按钮执行取消订阅和删除操作。

单击“订阅”按钮,我想删除该特定行。

你可以点击按钮的位置来找到indexPath,并从中找到有该按钮的单元格:

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

您可以获取单击按钮的位置,以查找indexPath,并从中查找具有该按钮的单元格:

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

当您在
cellForRowAtIndexPath
中创建行时,为按钮的标记指定一个indexath.row,最好添加一些数字,以避免与其他按钮混淆,例如:

cell.deleteButton.tag = 100 + indexPath.row

单击按钮时,将发送者强制转换为该按钮,恢复按钮的标记(记住减去100),您就知道要删除哪一行。

CellForRowatineXpath
中创建行时,为按钮的标记分配一个indexPath.row,最好添加一些数字,以避免与其他按钮混淆,例如:

cell.deleteButton.tag = 100 + indexPath.row

单击按钮时,将发送者强制转换为该按钮,恢复按钮的标记(记住减去100),您就知道要删除哪一行。

点击一行并删除它应该可以通过以下方式进行: 在
didselectRowatinedexpath
中,您可以实现以下功能:

[myArrayWithSubscriptions removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
如果只想点击UIButton删除,请在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];
这样,您就可以从点击了UIButton的单元格中获得indexPath,并且应该能够继续删除它。
我正在移动中键入此代码,稍后将检查此代码是否有任何打字错误。

点击一行并删除它应该可以这样做: 在
didselectRowatinedexpath
中,您可以实现以下功能:

[myArrayWithSubscriptions removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
如果只想点击UIButton删除,请在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];
这样,您就可以从点击了UIButton的单元格中获得indexPath,并且应该能够继续删除它。
我正在移动中键入此代码,稍后将检查此代码是否有任何打字错误。

a。将目标和回调添加到按钮(我想您已经这样做了)

b。在回调函数中,在SuperView上循环以查找UITableViewCell,然后查找其索引,如下所示:

-(void)deleteButtonPressed:(UIButton*)button {
    UIView* candidate = button;
    while (![candidate isKindOfClass:[UITableViewCell class]]) {
        candidate = candidate.superview;
    }
    NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:cell];
    ...
c。首先更新您的模型(非常重要)

d。调用删除行

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

a。将目标和回调添加到按钮(我想您已经这样做了)

b。在回调函数中,在SuperView上循环以查找UITableViewCell,然后查找其索引,如下所示:

-(void)deleteButtonPressed:(UIButton*)button {
    UIView* candidate = button;
    while (![candidate isKindOfClass:[UITableViewCell class]]) {
        candidate = candidate.superview;
    }
    NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:cell];
    ...
c。首先更新您的模型(非常重要)

d。调用删除行

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

您应该获得点击的按钮的
indexPath
。然后使用标准API调用删除选定的索引路径。像这样:

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

您应该获得点击的按钮的
indexPath
。然后使用标准API调用删除选定的索引路径。像这样:

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

将UIButton的子类设为CellButton,并采用如下自定义属性

@property (nonatomic,strong) NSIndexPath *indexPath;
将按钮的自定义类属性设置为在cellForRowAtIndexPath中创建的新属性

cell.btnSubscribe.indexPath=indexPath;
[cell.btnSubscribe addTarget:self action:@selector(subscribe:) forControlEvents:UIControlEventTouchUpInside];
现在在函数中

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

试一下这个答案。我确信这会起作用

将UIButton的子类作为CellButton,并采用如下自定义属性

@property (nonatomic,strong) NSIndexPath *indexPath;
将按钮的自定义类属性设置为在cellForRowAtIndexPath中创建的新属性

cell.btnSubscribe.indexPath=indexPath;
[cell.btnSubscribe addTarget:self action:@selector(subscribe:) forControlEvents:UIControlEventTouchUpInside];
现在在函数中

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

试一下这个答案。我确信这将工作

它与以下代码一起工作

点击按钮事件

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

它使用以下代码

点击按钮事件

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


@Bhavin你说的不工作是什么意思?你能把密码贴出来吗?我是说。我得到了一个标签好的,现在有了这个标签,如何删除这个单元格。只要确保你从按钮标签创建了indexPath,打印出标记值以确保其正确,并且您还必须从数据源中删除对象。我记录了标记值,它正确,因为我的订阅功能可以在每个索引上工作。是否从数据源中删除对象?你能发布所有你必须删除该单元格的代码吗?@Bhavin你说的“不工作”是什么意思?你能把密码贴出来吗?我是说。我得到了一个标签好的,现在有了这个标签,如何删除这个单元格。只要确保你从按钮标签创建了indexPath,打印出标记值以确保其正确,并且您还必须从数据源中删除对象。我记录了标记值,它正确,因为我的订阅功能可以在每个索引上工作。是否从数据源中删除对象?你能发布所有你必须删除单元格的代码吗?点击单元格内的按钮不会调用
tableView:didSelectRowAtIndexPath:
@codingVoldemort我想请你重新阅读我的答案。我从来没说过按一下按钮就可以了。阅读:点击一行并删除它应该可以这样做。。。我说划船。之后,我甚至提出了一个点击按钮的解决方案。否决票不好…点击单元格内的按钮不会调用
tableView:didSelectRowAtIndexPath:
@codingVoldemort我想请你重新阅读我的答案。我从来没说过按一下按钮就可以了。阅读:点击一行并删除它应该可以这样做。。。我说划船。之后,我甚至提出了一个点击按钮的解决方案。否决票不好…获得
UITableViewCell
参考的好方法!但是,你不认为这是一种过度的杀伤力吗?只是说:)在我的项目中,按钮有时嵌套在视图的深处,因此与其假设它是2或3级,不如使用一个实用函数,只返回任何子级的单元格。它看起来也比button.superview.superview.superview要好;-)考虑使用递归!移除对象