Ios 当高度改变时,如何在单元格中显示labelview的全文
我有一个带有自定义单元格的tableview,在我的单元格中,我有一个标签和一个图像,标签中的文本很长,所以我只显示前2行,然后使用换行符显示3点“…”,然后当用户点击单元格时,它将展开以显示标签的全文 我在录制时改变单元格的高度,我这样做:Ios 当高度改变时,如何在单元格中显示labelview的全文,ios,iphone,objective-c,uitableview,custom-cell,Ios,Iphone,Objective C,Uitableview,Custom Cell,我有一个带有自定义单元格的tableview,在我的单元格中,我有一个标签和一个图像,标签中的文本很长,所以我只显示前2行,然后使用换行符显示3点“…”,然后当用户点击单元格时,它将展开以显示标签的全文 我在录制时改变单元格的高度,我这样做: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([tableView indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView indexPathsForSelectedRows].count) {
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) {
MsgInfo *info = [_msgInfos objectAtIndex:indexPath.row];
NSString *displayString = info.msg;
CGRect boundingRect =
[displayString boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width - 2 * 10, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:
@{ NSFontAttributeName : [UIFont fontWithName:@"B Yekan" size:18.0f]} context:nil];
CGFloat rowHeight = ceilf(boundingRect.size.height);
NSLog(@"Height = %f for '%@'", rowHeight, displayString);
return 54 + rowHeight + 2 * 10; // Expanded height
}
return 92; // Normal height
}
return 92; // Normal height
}
因此,当我点击一个单元格时,它会根据我的标签文本字体和大小展开
但现在我想显示标签的全文,我知道我必须在didselectrowatinexpath中设置label.numberofline=0,但它不起作用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MsgCell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
[nameLabel setLineBreakMode:NSLineBreakByWordWrapping];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
有任何帮助吗?在
中,您正在创建一个新单元格并更改其设置(不编辑现有单元格,也不编辑任何即将用于显示的单元格)。您应该做的是触发所选单元的重新加载,以便重新计算新高度并重新加载单元。在tableView:cellforrowatinexpath:
中,您应该使用类似的逻辑来检查行是否被选中,然后设置标签上的行数
在tableView:cellforrowatinexpath:
中执行此操作还可以防止取消选择时出现问题,因为每次重复使用单元格时,必须始终为每个单元格设置行数。不应执行[[tableView indexpaths forselectedrows]indexOfObject:indexPath],因为indexPath是一个新创建的对象,可能不在selectedIndexPath数组中。我正在尝试您的解决方案,但在tableView:CellForRowIndexPath中该逻辑不起作用。您能再解释一下吗?看看您尝试了什么,知道它是如何起作用的,这会很有用。目的是设置行数。如果索引路径是选定的行,则设置为零,如果未选定,则设置为2。