我看到类似的问题几乎被问了几十次,但没有一个答案解决了我的问题(或者我太累了,所以我没有正确地遵循答案)
我有一个带有自定义单元格的表格视图。
细胞看起来像这样
该单元格是从 UITableViewCell 派生的 ResultsViewCell 类型。
单元格中的最后一个标签是多行单元格。那个有时会与下一个单元格的内容重叠。
这是我的 cellForRowAtIndexPath 函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"StandardSearchResultCell";
ResultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
Data *theData = [Data getInstance];
Company *theCompany = [theData.results objectAtIndex:indexPath.row];
cell.lblTitle.text = theCompany.DisplayName;
cell.lblDescription.text = theCompany.Description;
cell.lblAddressPt1.text = theCompany.AddressPt1;
cell.lblAddressPt2.text = theCompany.AddressPt2;
cell.lblPhone.text = theCompany.Phone;
cell.lblEmail.text = theCompany.Email;
cell.lblDescription.adjustsFontSizeToFitWidth = false;
cell.lblDescription.lineBreakMode = UILineBreakModeWordWrap;
cell.lblDescription.numberOfLines = 0;
[cell.lblDescription sizeToFit];
///////////////////////////////////////////
//edit -Added after David H's answer, but it didn't solve the problem
cell.contentView.clipsToBounds = false;
UIFont *cellFont = [UIFont systemFontOfSize:12.0];
CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
[cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, 270, 90 + labelSize.height)];
//end of edit
///////////////////////////////////////
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Data *theData = [Data getInstance];
Company *theCompany = [theData.results objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont systemFontOfSize:12.0];
CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return (90.0f + labelSize.height);
}
我究竟做错了什么?