2009-10-27 48 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 
          } 
         } 
        }