我在表格單元格的右側添加了一個日期時間標籤。當要刪除的記錄顯示「刪除」按鈕時,日期時間標籤需要稍微向左移動一點。但如何獲得「刪除」按鈕的大小?如何在刷卡刪除時獲取UITableViewCell中「刪除」按鈕的大小?
我試圖在cell.subviews中找到它,但失敗了。
我在表格單元格的右側添加了一個日期時間標籤。當要刪除的記錄顯示「刪除」按鈕時,日期時間標籤需要稍微向左移動一點。但如何獲得「刪除」按鈕的大小?如何在刷卡刪除時獲取UITableViewCell中「刪除」按鈕的大小?
我試圖在cell.subviews中找到它,但失敗了。
您不必知道按鈕的大小。而是使用單元格的contentView
屬性的大小來計算子視圖的大小。在單元格上滑動時,UIKit將調整contentView
的大小並在單元對象上調用layoutSubviews
。在您的UITableViewCell的子類中,覆蓋layoutSubviews
方法並將適當的大小設置爲子視圖。
看看蘋果的iPhoneCoreDataRecipes示例代碼的RecipeTableViewCell.m。
刪除按鈕是63x33。
但如果它是非英文的大小會改變。 – 2009-11-01 13:00:13
大小調整以適應所包含的文本。請看下面的代碼:
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Dynamic width!";
}
VS
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"⏎";
}
如果你不是我的做法是不要在您的自定義表視圖細胞重寫layoutSubviews方法:
autoresizingMask
至UIViewAutoresizingFlexibleWidth
。
使用此代碼在您的自定義單元格類
- (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
}
}
}
也許你並不需要知道你的目的的大小,所以我建議在我的答案的方法。 – DanSkeel 2012-09-05 16:10:09