2

我想为 uitableviewrowaction 按钮设置顶部和底部约束

这是我的代码

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

       }];
        deleteAction.backgroundColor = [UIColor redColor];

       return @[deleteAction];
  }

像这样我添加了删除按钮。在tableviewCell我添加了一个UIView它具有顶部和底部约束。我希望删除按钮与我在UITableviewCell.

在此处输入图像描述

4

3 回答 3

2

您可以在自定义 uitableviewcell 类中设置删除按钮框架

像这样

 -(void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super didTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {

        UIView *deleteButton = [self deleteButtonSubview:self];
        if (deleteButton)
        {
            CGRect frame = deleteButton.frame;
            frame.origin.y = 4;
            frame.size.height = frame.size.height-8;
            /*
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {

                frame.size.height = 62; //vikram singh 2/1/2015
                frame.size.width = 80;

            }
            else
            {
                frame.size.height = 52; //vikram singh 2/1/2015
                frame.size.width = 80;

            }
             */
            deleteButton.frame = frame;

        }
    }
}

- (UIView *)deleteButtonSubview:(UIView *)view
{
    if ([NSStringFromClass([view class]) rangeOfString:@"Delete"].location != NSNotFound) {
        return view;
    }
    for (UIView *subview in view.subviews) {
        UIView *deleteButton = [self deleteButtonSubview:subview];
        [deleteButton setBackgroundColor:[UIColor whiteColor]];
        if (deleteButton) {

            return deleteButton;
        }
    }
    return nil;
}

使用didTransitionToState方法:)

于 2017-02-03T11:28:33.903 回答
0
    super.willTransitionToState(state)
    if state == .ShowingDeleteConfirmationMask
    {
       let deleteButton: UIView? = subviews[0]

        if deleteButton != nil {
            var frame: CGRect? = deleteButton?.frame
            frame?.origin.y = 9
            frame?.origin.x = 10
            frame?.size.height = (frame?.size.height)! - 14
            deleteButton?.frame = frame!
        }
    }
于 2017-06-15T11:58:05.190 回答
0

关于巴尔卡兰的答案的更多信息......删除按钮是一个自定义的私有类,但使用该String(describing:)方法,您可以合理地确定您可以掌握它。
此外,我惊讶地发现didTransition一旦你开始改变状态就会触发,而不是在状态改变完成时触发。

Swift 3 中@balkaran 代码的更新版本:

override func didTransition(to state: UITableViewCellStateMask) {
    super.willTransition(to: state)
    if state == .showingDeleteConfirmationMask {
        let deleteButton: UIView? = subviews.first(where: { (aView) -> Bool in
            return String(describing: aView).contains("Delete")

        })
        if deleteButton != nil {
            deleteButton?.frame.size.height = 50.0
        }
于 2017-04-28T06:07:51.503 回答