[英]UITableViewCell resize to fit detail text
I am trying to resize a UITableViewCell
to show/hide parts of the detail text on selection. 我正在尝试调整UITableViewCell
大小以在选择时显示/隐藏详细信息的一部分。 All cells show the first line of the detail text. 所有单元格均显示详细信息的第一行。 when the user taps a cell, the entire detail text should be displayed without moving the currently visible part of the text. 当用户点击一个单元格时,应在不移动文本当前可见部分的情况下显示整个详细文本。
Currently, I am doing the following: 目前,我正在执行以下操作:
In cellForRowAtIndexPath:
在cellForRowAtIndexPath:
cell.textLabel.text = ...;
cell.detailTextLabel.text = ...;
if ([indexPath isEqual:self.currentSelection])
{
cell.detailTextLabel.numberOfLines = CGFLOAT_MAX;
}
else
{
cell.detailTextLabel.numberOfLines = 1;
}
In didSelectRowAtIndexPath:
在didSelectRowAtIndexPath:
if ([self.currentSelection isEqual:indexPath])
{
self.currentSelection = nil;
}
else
{
self.currentSelection = indexPath;
}
[tableView reloadData];
And in heightForRowAtIndexPath
并在heightForRowAtIndexPath
float minHeight = 60;
if ([indexPath isEqual:self.currentSelection])
{
KPLexiconEntry *lexiconEntry = ...;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary * attributes = @{NSFontAttributeName : cellFont, NSParagraphStyleAttributeName : paragraphStyle};
CGSize labelSize = [lexiconEntry.explanation boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
float height = labelSize.height + 44;
return height > minHeight ? height : minHeight;
}
return minHeight;
This shows or hides the detail text as i wanted to, but the text inside the cell (including the textLabel.text
) jumps up or down a few pixels. 这将显示或隐藏我想要的详细文本,但是单元格内的文本(包括textLabel.text
)会向上或向下跳跃几个像素。
How can I prevent this and get a smooth appearance? 如何防止这种情况并使外观光滑?
You Can do it :- 你能行的 :-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifire = @"CellIdentifire";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
[cell.textLabel sizeToFit];
}
cell.textLable.text= @"";
return cell;
}
You can add height constraint for details text (hiding element) and outlet him. 您可以为详细信息文本(隐藏元素)添加高度限制,然后将其输出。 Then set when that element is hidden his constraint == 0; 然后在隐藏该元素时设置其约束== 0;
I figured out a way to solve this: 我想出了一种解决方法:
Instead of using the detailLabel
, I created a NSAttributedString
and used textLabel.attributedText
. 我没有使用detailLabel
,而是创建了NSAttributedString
并使用了textLabel.attributedText
。 The attributed string simply shows the title (previously in the textLabel
) in a larger font than the rest (previously ind the detailLabel
). 属性字符串textLabel
比其余字体(以前是textLabel
)大的字体显示标题(以前在detailLabel
)。 I created a method to create the attributed string: 我创建了一个方法来创建属性字符串:
- (NSAttributedString*)attributedTextForIndexPath:(NSIndexPath*)indexPath {
id itemForIndexPath = ...;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
UIFont *titleFont = [UIFont systemFontOfSize:16];
NSDictionary *titleAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: titleFont};
NSString *titleRaw = [NSString stringWithFormat:@"%@\n", @"itemForIndexPath.title"];
NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleRaw attributes:titleAttributes];
[attrString appendAttributedString:title];
UIFont *textFont = [UIFont systemFontOfSize:14];
NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: textFont};;
NSString *textRaw = itemForIndexPath.text;
if (![indexPath isEqual:self.currentSelection] && textRaw.length >= 30)
{
textRaw = [NSString stringWithFormat:@"%@...", [textRaw substringToIndex:27]];
}
NSAttributedString *text = [[NSAttributedString alloc] initWithString:textRaw attributes:textAttributes];
[attrString appendAttributedString:text];
return attrString;
}
As you can see, I manually restrict the length of the text for all but the selected cell. 如您所见,我手动限制了除选定单元格之外的所有单元格的文本长度。
Then, in cellForRowAtIndexPath
, I assign it like this: 然后,在cellForRowAtIndexPath
,我将其分配为:
cell.textLabel.attributedText = [self attributedTextForIndexPath:indexPath];
and in heightForRowAtIndexPath
: 并在heightForRowAtIndexPath
:
NSAttributedString *text = [self attributedTextForIndexPath:indexPath];
CGSize constraintSize = CGSizeMake(255, CGFLOAT_MAX);
CGSize size = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
return ceil(size.height + 22); // adding 22 for padding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.