2009-10-27 47 views
1

我在表格单元格的右侧添加了一个日期时间标签。当要删除的记录显示“删除”按钮时,日期时间标签需要稍微向左移动一点。但如何获得“删除”按钮的大小?如何在刷卡删除时获取UITableViewCell中“删除”按钮的大小?

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

+0

也许你并不需要知道你的目的的大小,所以我建议在我的答案的方法。 – DanSkeel 2012-09-05 16:10:09

回答

3

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

看看苹果的iPhoneCoreDataRecipes示例代码的RecipeTableViewCell.m

-1

删除按钮是63x33。

+3

但如果它是非英文的大小会改变。 – 2009-11-01 13:00:13

1

大小调整以适应所包含的文本。请看下面的代码:

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

VS

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

如果你不是我的做法是不要在您的自定义表视图细胞重写layoutSubviews方法:

  1. 创建自定义子视图,基于contentView.bounds设置框架。
  2. 设置autoresizingMaskUIViewAutoresizingFlexibleWidth
  3. 将您的自定义子视图添加到单元格的ContentView中。对于editing

  • 配置单元现在,当你刷对细胞的删除按钮出现,您的看法与自动调整大小内容查看。

  • 2

    使用此代码在您的自定义单元格类

    - (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 
          } 
         } 
        }