通过单击自定义UIButton删除UITableView行

基本上我想删除行上的一个button,这是该行的一部分单击事件。
我不能使用提交编辑风格,因为我想执行取消订阅和删除相同的button。
在这里输入图像说明

点击订阅button,我想删除特定的行。
所以任何想法,我怎么能做到这一点。

它适用于以下代码

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

一个。 添加目标和callback到button(我认为你已经这样做了)

湾 在callback函数中,遍历superviews来查找UITableViewCell,然后find它的索引,如下所示:

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

C。 先更新你的模型(非常重要)

 [self.dataArrayThatFeedsTableView removeObjectAtIndex:indexPathToDelete.item] 

d。 调用删除行

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

当你在cellForRowAtIndexPath创build行时,将indexPath.row分配给button的标签,好主意是添加一些数字以避免与其他button混淆,例如:

 cell.deleteButton.tag = 100 + indexPath.row 

当button被点击时,将发件人转换到button,恢复button的标签(记得减去100),并且知道要删除哪一行。

你可以得到button的位置,以findindexPath,从那里find有该button的单元格:

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

你应该得到被点击的button的indexPath 。 然后使用标准API调用删除选定的索引path。 像这样:

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

使用像这样的自定义属性作为CellButton的UIButton的子类

 @property (nonatomic,strong) NSIndexPath *indexPath; 

将button的自定义类属性设置为新build的button,并在cellForRowAtIndexPath中设置

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

现在在function

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

试试这个答案。 我确定这会工作

点击一行并删除它应该可能是这样的:在你didSelectRowAtIndexPath你可以实现以下内容:

 [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,应该能够继续删除它。 我在旅途中打字,稍后会检查这个代码是否有错别字。