12

我想在我的单元格上添加一个与滑动删除功能相同的自定义按钮。因此,当单击我的自定义按钮时,此按钮将被隐藏,以显示官方的红色“删除”按钮。

所以我做了这样的事情:

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

此时,我的自定义按钮在单击它们时会被隐藏,但没有出现官方的红色“删除”按钮。

有人知道如何处理吗?

4

3 回答 3

5

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

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

    [tableView setEditing:YES animated:YES];
}

所以你可能需要做一些事情,比如将 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;
}
于 2013-03-18T15:44:40.003 回答
1

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

如果你想知道如何替换默认减号(删除)编辑控件,有一篇不错的文章,我推荐:http: //vinsol.com/blog/2015/01/06/custom-edit-control-for-合适的viewcell/

因此,假设您从文章中制作了第一部分,并且您希望在按下自定义编辑控件按钮时看到删除按钮。代码在 Swift 上,但您可以使用相同的步骤在 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    
        }

    })


}

我们不想移动内容视图,因为我发现如果我们的按钮不在内容视图上方,它是不可触摸的,我有一些想法为什么,但不确定。如果您知道原因,请在评论中添加此信息,我很想知道。

我还想指出,如果您像我一样使用自动布局,则需要更改动画中的约束,而不是帧,因此您可能不需要extend部分。

于 2015-08-27T20:03:11.670 回答
-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];
    }
}
于 2013-05-10T06:39:56.837 回答