В моих 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, похоже, тоже зависит от некоторых textLAbels (в зависимости от количества строк), выдвигаемых в несколько пикселей.Я пытался установить значения отступов, но это не работает.Кто-нибудь еще сталкивался с этим?
РЕДАКТИРОВАТЬ: я добавляю метод layoutSubviews моего подкласса UITableViewCell, который я использую (без 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;
}
