2013-03-18 72 views
12

我想在我的单元格上添加一个自定义按钮,与滑动删除功能做同样的事情。所以当点击我的自定义按钮时,这个会隐藏起来让官方出现红色的“删除”按钮。以编程方式触发UITableViewCell“删除”按钮

所以我做了这样的事情:

/// Controller.m 
/// 
/// @brief Delete icon button pressed. Trigger display of Delete full button 
/// 
- (IBAction)deleteDrug:(id)sender event:(id)event { 
    NSIndexPath *indexPath = [self indexPathForButton:sender event:event]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    [cell setEditing:YES animated:YES]; 
} 

/// CustomCell.m 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 

    // hide/show "modify" button when entering in edit mode 
    switch (editing) { 
     case YES: 
      self.deleteButton.hidden = YES; 
      break; 
     case NO: 
      self.deleteButton.hidden = NO; 
      break; 
     default: 
      break; 
    } 
} 

在这一刻,当你点击它们,但官方的红色“删除”按钮并未显示在我的自定义按钮越来越隐藏。

做一个人知道如何处理这个?

回答

5

我相信删除按钮更多地由tableView处理。因此,而不是设置你的单元格编辑,你可能需要让tableView知道它应该是编辑。

- (IBAction)deleteDrug:(id)sender event:(id)event { 
    selectedButtonIndex = [self indexPathForButton:sender event:event]; 

    [tableView setEditing:YES animated:YES]; 
} 

所以你可能需要做一些事情,比如设置tableView来编辑。然后在你的tableview的数据源中,你可以实现这个方法,其中selectedButton是适当单元格的索引路径。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath == selectedButtonIndex) { 
     return YES; 
    } 

    return NO; 
} 

您可能需要为您的数据源实现此方法。

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
+6

'[tableView setEditing:YES animated:YES];'会在单元格左边放置一个红色图标(允许您调出右边的红色“删除”按钮)。我不想要它。我真的希望我的自定义按钮直接显示右边的红色“删除”按钮。 – Yaman 2013-03-18 15:57:13

+0

我想这是真的。它可能是因为你试图覆盖预期的行为,你可能需要去全距离并处理删除按钮。 – 2013-03-18 15:58:11

+0

叶我害怕这将是唯一的方式... Thx为你的帮助瑞安。 – Yaman 2013-03-18 16:00:59

-3

请执行此操作以在UITableViewcell的右侧使用您的删除按钮。

- (void)tableView:(UITableView *)tableView 
     commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
        withRowAnimation:UITableViewRowAnimationTop]; 
     [self.**yourarray** removeObjectAtIndex:indexPath.row]; 
     [tableView endUpdates]; 
     [tableView reloadData]; 
    } 
} 
+0

不适当的答复。 – 2015-09-05 08:23:07

+0

不回答问题。 – Camsoft 2016-11-10 09:14:37

1

我没有找到任何适当的解决方案来解决这个问题,所以我做了我自己的。我做了假删除按钮,它的工作原理非常像原来的那个。我在这里显示棘手的删除按钮部分。

如果你想知道如何更换默认减去(删除)编辑控件,有关于它的好文章,我建议:http://vinsol.com/blog/2015/01/06/custom-edit-control-for-uitableviewcell/

所以,假设你发文章第一部分,你想看到按下自定义编辑控制按钮时删除按钮。该代码是雨燕,但您可以使用相同的步骤,使这个在Objective-C

func deleteButtonShow(){ 

    let deleteButton = UIButton(frame: CGRect(x: self.frame.width, y: 0, width: 80, height: self.frame.height)) 
    deleteButton.backgroundColor = UIColor.redColor() // ! note: original delete have another color 
    deleteButton.setTitle("Delete", forState: UIControlState.Normal) 
    deleteButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    deleteButton.addTarget(self, action: "actionForDelete", forControlEvents: UIControlEvents.TouchUpInside) 
    // action for your button will call delegate that will do the delete work. in most cases 
    self.contentView.addSubview(deleteButton) 

    var movedSubviews = self.subviews   
    var i = 0 
    for subview in movedSubviews { 
     // NOTE: I added tag = 1 in the storyboard to Content View of my cell 
     if subview.tag == 1 { 
      movedSubviews.removeAtIndex(i) // we don't need to move it. 
      break 
     } 
     ++i 
    } 
    movedSubviews.extend(self.contentView.subviews)// add subviews from content view 

    UIView.animateWithDuration(0.3, animations: {() -> Void in 

     for movedView in movedSubviews as! [UIView] { 

      movedView.frame = CGRect(origin: CGPoint(x: movedView.frame.origin.x - 80, y: movedView.frame.origin.y), size: movedView.frame.size) 
     //animate the things  
     } 

    }) 


} 

我们不希望因为我发现移动内容的观点,即如果我们的按钮是不是在内容上面认为它是不可触摸的,我有一些想法为什么,但不知道。如果你知道为什么,请在评论中添加此信息,我很想知道它。

我还想指出,如果您使用autolayout作为我,您需要更改动画约束,而不是帧,所以您可能不需要extend部分。

相关问题