在我的UITableViewCells中,我顯示了不同長度的文本。爲了容納更大數量的文本,並同時不要求小文達在巨大的表格單元格,我在這裏設置行的高度...UITableViewCells的不同高度的標籤改變標籤的寬度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
float padding = 40.0f;
CGSize constraintSize;
constraintSize.width = 320.0f - padding - padding;
constraintSize.height = MAXFLOAT;
CGSize theSize = [thisrowstext sizeWithFont:[UIFont systemFontOfSize:14.0f]
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
if(theSize.height < 24.0f) return 44.0f;
else return theSize.height + 20.0f;
}
...它工作得很好,不幸的是,爲textLabel的寬度似乎也受到一些文本標誌(取決於行數)在幾個像素中的影響。我嘗試過設置縮進值,但這不起作用。有人遇到過這種情況麼?
編輯:我添加了我使用的UITableViewCell子類的layoutSubviews方法(沒有NIB)。
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.hideImage)
{
self.imageView.alpha = 0.0f;
self.imageView.frame = CGRectMake(-40.0f, 1.0f, 40.0f, 40.0f);
CGRect frame = self.textLabel.frame;
self.textLabel.frame = CGRectMake(frame.origin.x - 40.0f, frame.origin.y, frame.size.width + 40.0f, frame.size.height);
[self.textLabel setNeedsLayout];
}
else
{
self.imageView.alpha = 1.0f;
self.imageView.frame = CGRectMake(1.0f, 1.0f, 40.0f, 40.0f);
[self.textLabel setNeedsLayout];
}
}
編輯:另外增加的cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableCellViewWithHidableImageIdentifier = @"TableCellViewWithHidableImage";
TableCellViewWithHidableImage *cell = (TableCellViewWithHidableImage *)[tableView dequeueReusableCellWithIdentifier:TableCellViewWithHidableImageIdentifier];
if (cell == nil)
{
cell = [[[TableCellViewWithHidableImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableCellViewWithHidableImageIdentifier] autorelease];
}
cell.hideImage = NO;
cell.imageView.image = [UIImage imageNamed:@"empty.png"];
cell.textLabel.font = [UIFont systemFontOfSize:16.0f];
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = @"whatever";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
良好的通話,讓我玩這個,謝謝。 – rob5408 2011-05-14 23:27:52
我將單元格的背景設置爲灰色以更好地查看發生了什麼。仔細查看單元格的佈局這些子目錄讓我得出了答案。 textLabel縮小了框架的寬度,然後「漂浮」到正確的位置。我無法弄清楚在單元格上改變什麼屬性使其「左」浮動,因此我手動將框架的原點x設置爲0。 – rob5408 2011-05-15 21:41:16