首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义UITableViewRowAction按钮

自定义UITableViewRowAction按钮
EN

Stack Overflow用户
提问于 2017-02-03 11:01:56
回答 3查看 2.8K关注 0票数 2

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

这是我的密码

代码语言:javascript
复制
- (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中的视图相匹配。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-03 11:28:33

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

像这样

代码语言:javascript
复制
 -(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方法:)

票数 2
EN

Stack Overflow用户

发布于 2017-04-28 06:07:51

更多关于巴尔干半岛的答案。delete按钮是一个自定义的私有类,但是使用String(describing:)方法,您可以相当肯定您可以获得它。

而且,我很惊讶地发现,当您开始更改状态时,而不是当状态完成更改时,didTransition就会触发。

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

代码语言:javascript
复制
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
        }
票数 0
EN

Stack Overflow用户

发布于 2017-06-15 11:58:05

代码语言:javascript
复制
    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!
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42022696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档