滑动删除时如何获取UITableViewCell中“删除”按钮的大小?

时间:2009-10-27 08:00:19

标签: iphone uitableview

我在表格单元格的右侧添加了日期时间标签。当swip-to-delete显示“删除”按钮时,日期时间标签需要向左移动一点。但是如何获得“删除”按钮的大小?

我试图在cell.subviews中找到它但却失败了。

5 个答案:

答案 0 :(得分:3)

您不必知道按钮的大小。而是使用单元格的contentView属性的大小来计算子视图的大小。在单元格上滑动时,UIKit将调整contentView的大小并在单元格对象上调用layoutSubviews。在UITableViewCell的子类中,覆盖layoutSubviews方法并为子视图设置适当的大小。

查看Apple的iPhoneCoreDataRecipes示例代码的RecipeTableViewCell.m

答案 1 :(得分:2)

在自定义Cell类

中使用此代码
 - (void)layoutSubviews {
        [super layoutSubviews];
        NSMutableArray *subviews = [self.subviews mutableCopy];
        while (subviews.count > 0)
        {
            UIView *subV = subviews[0];
            [subviews removeObjectAtIndex:0];
            if ([NSStringFromClass([subV class])isEqualToString:@"UITableViewCellDeleteConfirmationView"]) 
           {
            UIView *deleteButtonView = (UIView *)[self.subviews objectAtIndex:0];
            CGFloat  deleteBtnHeight=deleteButtonView.frame.size.height;//here you get the height
             }
        }
    }

答案 2 :(得分:1)

调整大小以适合包含的文本。请参阅以下代码:

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Dynamic width!";
}

VS

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"⏎";
}

答案 3 :(得分:0)

如果您没有覆盖自定义表格视图单元格中的layoutSubviews方法而不是我的方法:

  1. 创建自定义子视图,根据contentView.bounds设置框架。
  2. autoresizingMask设为UIViewAutoresizingFlexibleWidth
  3. 将自定义子视图添加到单元格的ContentView。
  4. 配置editing
  5. 的单元格

    现在,当您在单元格上滑动时,会出现删除按钮,并且您的视图会自动调整为contentView。

答案 4 :(得分:-1)

删除按钮为63x33。

相关问题