问题:
我在表格单元格中有一个标签,如下所示:
编辑:虽然 tableview 不使用 AutoLayout,但单元格的 xib 却使用。这是它的样子:
现在,“...”表示评论(文本)太长而无法显示。然后用户可以单击单元格,这会向下展开单元格和标签,然后会显示其余文本。
这是 heightForRowAtIndexPath
的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if (tableView == self.rateAndReviewView.ratingsTable) {
BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
CGFloat maxHeight = MAXFLOAT;
CGFloat minHeight = 40.0f;
if (isSelected){
CGFloat constrainHeight = isSelected?maxHeight:minHeight;
CGFloat constrainWidth = tableView.frame.size.width - 20.0f;
//
SQLReview *review = [reviewsArray objectAtIndex:indexPath.row];
NSString *text = review.comment;
CGSize constrainSize = CGSizeMake(constrainWidth, constrainHeight);
CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:15.0f]
constrainedToSize:constrainSize
lineBreakMode:NSLineBreakByWordWrapping];
CGFloat labelHeight = labelSize.height;
return MAX(labelHeight+20, 100.0f);
} else {
minHeight = 100.0f;
return minHeight;
}
}
}
这是 cellForRowAtIndexPath
的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.rateAndReviewView.ratingsTable) {
static NSString *CellIdentifier = @"Cell";
VenueReviewCell *cell = (VenueReviewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell = [[[NSBundle mainBundle] loadNibNamed:@"ReviewCell" owner:nil options:nil] objectAtIndex:0];
}
[cell.contentView.layer setBorderWidth:0.5f];
cell.contentView.backgroundColor = [UIColor whiteColor];
SQLReview *review = [reviewsArray objectAtIndex:indexPath.row];
cell.usernameLabel.text = (review.user_name == nil) ? @"" : review.user_name;
cell.datetimeLabel.text = (review.datetime == nil) ? @"" : [review.datetime substringToIndex:10];
cell.commentLabel.text = (review.comment == nil) ? @"" : review.comment;
cell.commentLabel2.text = (review.comment == nil) ? @"" : review.comment;
float overallScore = (review.overall == nil) ? 0.0f : [review.overall floatValue];
NSArray *overallImageViewArray = @[cell.overallStarImageView1, cell.overallStarImageView2, cell.overallStarImageView3, cell.overallStarImageView4, cell.overallStarImageView5];
if([selectedIndexPaths containsObject:indexPath]) {
cell.commentLabel.hidden = YES;
cell.commentLabel2.hidden = NO;
CGSize commentLabelComputeSize = [cell.commentLabel2.text sizeWithFont:cell.commentLabel2.font constrainedToSize:CGSizeMake(cell.commentLabel2.frame.size.width, CGFLOAT_MAX) lineBreakMode:cell.commentLabel2.lineBreakMode];
CGFloat commentLabelHeightOffset = (commentLabelComputeSize.height - cell.commentLabel2.frame.size.height > 0) ? (commentLabelComputeSize.height - cell.commentLabel2.frame.size.height) : 0;
cell.commentLabel2.frame = CGRectMake(cell.commentLabel2.frame.origin.x, cell.commentLabel2.frame.origin.y, cell.commentLabel2.frame.size.width, cell.commentLabel2.frame.size.height + commentLabelHeightOffset);
cell.commentLabel2.autoresizingMask = UIViewAutoresizingFlexibleHeight;
cell.commentLabel2.numberOfLines = 0;
} else {
cell.commentLabel.hidden = NO;
cell.commentLabel2.hidden = YES;
}
[CommonUtilities displayStarRatingsWithScore:overallScore starImageViewArray:overallImageViewArray];
return cell;
}
return nil;
}
heightForRowAtIndexPath
中的 isSelected
部分确实有效,并且表格单元格确实在点击时明显展开,但文本保持不变 - 至少,在 iOS 7 上进行了测试+ 设备。
编辑:我还注销了 cell.commentLabel2.frame
的值,方法是添加:
NSLog(@"VenueTabViewController: cellForRow: contains indexpath %@, size: %@", indexPath, NSStringFromCGRect(cell.commentLabel2.frame));
在 cellForRowAtIndexPath
的 if
和 else
中,显示如下:
第一次点击:
VenueTabViewController:cellForRow:包含索引路径 {length = 2,path = 0 - 1},大小:{{20, 31}, {285, 60.579999999999998}}
第二次点击:
VenueTabViewController:cellForRow:包含索引路径{length = 2,path = 0 - 2},大小:{{20, 31}, {285, 21}}
所以它的大小确实发生了变化。
我尝试了什么:
我在谷歌上搜索了一下,发现 [string sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:constrainSize lineBreakMode:NSLineBreakByWordWrapping];
已经贬值了。因此,我将上面与之相关的行替换为:
CGSize labelSize = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
CGFloat labelHeight = ceilf(labelSize.height);
这不仅没有起作用,还阻止了细胞的扩张,我认为这可能是一种倒退。
我还尝试添加以下内容,因为为什么不:
VenueReviewCell *cell = (VenueReviewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell = [[[NSBundle mainBundle] loadNibNamed:@"ReviewCell" owner:nil options:nil] objectAtIndex:0];
}
[cell.commentLabel sizeToFit];
但还是不行。
然后,我简单地将 commentLabelHeightOffset
替换为 100,只是为了测试是否有任何变化:
cell.commentLabel2.frame = CGRectMake(cell.commentLabel2.frame.origin.x, cell.commentLabel2.frame.origin.y, cell.commentLabel2.frame.size.width, cell.commentLabel2.frame.size.height + commentLabelHeightOffset);
什么都没发生。
编辑:这是我所做的,最终获得了关注 - 我按照 saif 的建议删除了单元格上的自动版式。现在,虽然细胞确实在膨胀,标签也随之膨胀,但出于某种原因,我得到了这个:
我检查了代码,标签框架的 X 和 Y 没有改变(只是框架高度)并且文本在实际文本之前没有“新行”。
请问有什么帮助吗?
最佳答案
试试这个,将框架设置为行单元格中的 desc 标签(取决于文本大小),并在要展开单元格时重新加载表格 View 。
在cellForRowAtIndexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//code to add desc label
}
CGSize suggestedSize = [descriptionText sizeWithFont:descLabel.font constrainedToSize:CGSizeMake(descLabel.frame.size.width, FLT_MAX) lineBreakMode:descLabel.lineBreakMode];
float padding = 20;
[descLabel setFrame:CGRectMake(padding, CGRectGetMaxY(nameLabel.frame), CGRectGetWidth(tableView.frame)-(2*padding), suggestedSize.height)];
如果您在自定义单元格中使用自动布局,那么
- 保留备份,并取消选中自动布局
- 在下拉菜单中选择 iPhone,然后选择禁用尺寸等级
如果行高有问题,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize suggestedSize = [descriptionText sizeWithFont:descLabel.font constrainedToSize:CGSizeMake(descLabel.frame.size.width, FLT_MAX) lineBreakMode:descLabel.lineBreakMode];
float heightOfRow = CGRectGetMaxY(nameLabel.frame)+suggestedSize.height+5; // height of name label(as it is single line supported) + height of desc label + bottom padding
return heightOfRow;
}
希望对你有帮助
关于ios - Xcode 6 : Label in Table Cell won't expand/display longer text upon expansion/line up with cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471301/