我的自定义UITableView
出现问题。我想知道如何正确地将一组文本放入单元格而不会看到任何省略号“......”并且没有文本在单元格的末尾被截断。
这就是我的手机目前的样子:
它是UISplitViewController
的一部分。这个问题是,之前由于某种原因,它会显示文本的整个长度,但它会到达单元格的末尾,而其余的字符串被切断(当我检查“AutoLayout”时会发生这种情况)。
这是我目前的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BCell";
BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[BracketTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.description setLineBreakMode:NSLineBreakByWordWrapping];
cell.description.numberOfLines = 0;
cell.description.font = [UIFont fontWithName:@"Helvetica" size:14.0];
}
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
[cell.description setText:bracket.name];
[cell.bracketId setText:[NSString stringWithFormat:@"%@", bracket.bracketId]];
return cell;
}
我正在尝试高度,但这似乎并不重要,因为我可以将高度设置为任何值,但它仍然显示截断的文本。
谢谢!
答案 0 :(得分:1)
通常我支持变量高度单元格的方法是定义一个可以计算给定模型对象大小的类方法:
+ (CGFloat)heightForBracket:(Bracket*)bracket;
使它成为类方法的美妙之处在于,您可以与实际实现布局的代码共享常量(填充值,字体大小,缩进级别等),而不必将它们暴露给任何其他类。如果您希望将来更改这些常量,则只需在单元子类中的一个位置进行更改。子类实现的示例:
#define kPaddingHorizontal 10.0
#define kPaddingVertical 10.0
#define kFontSizeName 17.0
+ (CGFloat)heightForBracket:(Bracket*)bracket {
// determine the dimensions of the name
UIFont *nameFont = [UIFont systemFontOfSize:kFontSizeName];
CGFloat nameSize = [bracket.name sizeWithFont:nameFont
constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) // 300 is the width of your eventual label
lineBreakMode:NSLineBreakByWordWrapping];
// Apple recommends all cells be at least 44px tall, so we enforce a minimum here
return MAX(44, nameSize.height + 20 + kPaddingVertical*2); // 20 is space for the subtitle label
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
// bracket name
self.textLabel.numberOfLines = 0; // 0 makes this variable height
self.textLabel.font = [UIFont systemFontOfSize:kFontSizeName];
self.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.textLabel.backgroundColor = [UIColor clearColor];
// if you wanted to hardcode a specific width, to a subview do it here as a constant and then share it with heightForBracket:
// bracket number
self.detailTextLabel.numberOfLines = 1;
self.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
self.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setBracket:(Bracket*)bracket {
_bracket = bracket;
self.textLabel.text = bracket.name;
self.detailTextLabel.text = [NSString stringWithFormat:@"%@", bracket.bracketId];
}
然后,您可以在heightForBracket:
中致电tableView:heightForRowAtIndexPath:
:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
return [BracketTableCell heightForBracket:bracket];
}
tableView:cellForRowAtIndexPath:
变得非常简单,只需在单元格上设置适当的括号:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"BCell";
BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[BracketTableCell alloc] initWithReuseIdentifier:CellIdentifier];
}
Bracket *bracket = [brackets objectAtIndex:indexPath.row];
cell.bracket = bracket;
return cell;
}
一些注意事项:
description
,因为这是method that already exists on the NSObject
protocol heightForBracket:
的结果以提高滚动性能,特别是如果您开始为大量子视图调整逻辑逻辑答案 1 :(得分:0)
@gdubs你可以使用自定义UITableViewCells
供参考,您可以使用Customize Table View Cells for UITableView
我想你可以很容易地自定义UILabels。就像你想添加mutilple行然后设置TitletLabel.numberOfLines=0;
并且如果你想要wordwrapping TitleLabel.lineBreakMode=NSLineBreakByWordWrapping;
。自动换行还有其他选择。
答案 2 :(得分:0)
标签和Autolayout幸福的关键是在标签上设置preferredMaxLayoutWidth
属性。如果没有这个标签,请不要正确包装(或根本没有,在某些情况下,这是您之前看到的,我认为?)。
将值设置为最大线宽,然后标签应该正常运行。
答案 3 :(得分:0)
我认为问题与标签的宽度有关,如果您使用自动布局,请展开标签的宽度以填充父单元格并添加尾随并导致超级视图< / strong>约束,以便随之调整大小。