[英]Resizing custom UITableViewCell's subviews on creation
我試圖用其自己的XIB創建一個自定義UITableViewCell,其中包含兩個重要的可編輯組件:
創建UITableViewCell時,可以輕松設置其標簽的文本,但是在同一方法中,我還嘗試更改UIImage的框架以匹配UILabel的框架。 但是,直到我通過滾動表格視圖以使其出隊並將其放回視圖中來“重新加載”該單元格時,才會發生這種情況。
我在視圖控制器的.m中創建自定義表格視圖單元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *UITableViewCellIdentifier = @"msgCell";
RMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:UITableViewCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MessageCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
[cell customInit]; //This sets up the ImageView's image etc
}
[cell setMessageValue:[_messages objectAtIndex:indexPath.row]]; //SEE BELOW
return cell;
}
...然后在我的自定義單元格的.m中,我使用以下方法處理設置文本標簽並調整圖像大小:
- (void)setMessageValue:(NSString *)message {
_testLabel.text = message;
// Assume the text label's frame has already been set to the message's length
// (will add this in once I figure out how to change the imageView's
// frame correctly)
// Set the image view's frame to the label's
// THIS DOES NOT TAKE EFFECT UNTIL THE TABLE VIEW IS RE-SCROLLED
CGRect frame = _imageView.frame;
frame.size.width = _testLabel.frame.size.width + 8;
_imageView.frame = frame;
}
這是因為表視圖單元格以一種尺寸創建的,然后在將其添加到表中后顯示時對其進行了調整。 如果記錄單元格的邊界或框架,則從筆尖加載該單元格時,它將看到一個尺寸(較小),而通過滾動到屏幕之外來“刷新”該單元格,則將看到另一個尺寸(較大)。然后繼續。
最簡單的方法是確保標簽和圖像都具有相同的自動調整大小規則,以便在調整單元格的大小時,它們都可以一起生長。
您可以使用UITableView委托方法,
– (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
在其中,您可以動態設置行的高度。
//這僅是示例,您可以設置單元格高度
-(CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text;
text = [_messages objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(280, 2000);
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height + 5;
}
如果以編程方式創建子視圖並將其添加到表視圖單元中,則應該能夠控制子視圖的大小(即,這樣子視圖不會在情節提要中設置,因此不會受到明確的自動約束)。
在我的實現中,我創建了一個customCell子類。 在.h中:
@property (strong, nonatomic) UILabel *customLabel;
然后在.m中:
- (id)initWithCoder:(NSCoder *)aDecoder {
METHOD;
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[self addSubview:self.customLabel];
}
return self;
}
在tableViewController的.m文件中,在tableView:cellForRowAtAtIndexPath方法中:
theCell.customLabel.frame = CGRectMake(0, 0, 320, 40);
theCell.customLabel.text = @"Whatever text you want";
當加載表格視圖和單元格時,這將使您立即對自定義標簽進行完全控制。
作為對@inafziger的響應,我嘗試以編程方式刪除標簽上的約束,但無法解決此自動調整大小的問題。
更新:臨時引用子視圖,然后將其從超級視圖中刪除,然后將其重新添加到子視圖中,效果也一樣,但在我使用標簽的情況下,高度略有例外標簽的高度適合其所包含文本的高度(而在上面的示例中以編程方式創建標簽,標簽將具有您為其指定的高度。)優點是您無需創建任何自定義標簽,甚至觸摸子類tableviewCell。 只需使用您在情節提要中創建的任何內容即可。 這是tableView:cellForRowAtIndexPath:
UILabel *theLabel = theCell.theLabel;
[theCell.theLabel removeFromSuperview];
[theCell addSubview:theLabel];
theCell.theLabel.frame = CGRectMake(0, 0, 320, 40);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.