uitableviewcell文本标签太长,将detailtextlabel推出视图

| 当我使用UITableViewCellStyleValue1时,我得到了一长串的textLabel,并且不知何故,detailTextLabel从视图中推出了。 当我缩短textLabel文本时,可以看到detailTextLabel \的文本。 无论如何,是否以上述样式限制了textLabel的宽度,以至于它会用太长的时间截断textLabel? 我的代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @\"Cell\";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

}

cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;

//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [self.currencyNameIndex objectAtIndex:[indexPath section]];

//---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@\"SELF beginswith[c] %@\", alphabet];
self.currencyList = [self.keyCurrencyName filteredArrayUsingPredicate:predicate];

if ([self.currencyList count] > 0) 
{
    NSString *currencyName = [self.keyCurrencyName objectAtIndex:indexPath.row];
    cell.textLabel.text = currencyName;

    NSString *currencyCode = [self.valueCurrencyCode objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = currencyCode;

}   

return cell;
}
所以我的货币名称在某些条目上会很长。     
已邀请:
使用UILabel
lineBreakMode
属性将文本限制在restrict2ѭ的宽度内
@property(nonatomic) UILineBreakMode lineBreakMode
如下使用。
myLabel.lineBreakMode = UILineBreakModeTailTruncation;
这是可以与
lineBreakMode
一起使用的值的列表。 http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/c_ref/UILineBreakMode 编辑: 根据需要设置
UILabel
的宽度 例如。
myLabel.frame.size.width = 320;
    
对我来说,最简单的方法是将UITableViewCell子类化并覆盖layoutSubviews。 无法找到一种可靠的方法来仅根据标签框架来计算位置,因此仅对附件宽度进行硬编码,这种情况下是使用UITableViewCellAccessoryDisclosureIndicator附件类型的UITableViewCellStyleValue1单元格。
- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat detailTextLabelWidth = [self.detailTextLabel.text sizeWithFont:self.detailTextLabel.font].width;
    CGRect detailTextLabelFrame = self.detailTextLabel.frame;

    if (detailTextLabelFrame.size.width <= detailTextLabelWidth && detailTextLabelWidth > 0) {
        detailTextLabelFrame.size.width = detailTextLabelWidth;
        CGFloat accessoryWidth = (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) ? 28.0f : 35.0f;
        detailTextLabelFrame.origin.x = self.frame.size.width - accessoryWidth - detailTextLabelWidth;
        self.detailTextLabel.frame = detailTextLabelFrame;

        CGRect textLabelFrame = self.textLabel.frame;
        textLabelFrame.size.width = detailTextLabelFrame.origin.x - textLabelFrame.origin.x;
        self.textLabel.frame = textLabelFrame;
    }
}
    
@Jhaliya @卢卡斯
cell.textLabel.numberOfLines = 3; // set the numberOfLines
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
参见此处:自定义UITableViewCell。无法应用UILineBreakModeTailTruncation     
我遇到了同样的问题,不得不创建一个UITableViewCell子类。这样做比我想象的要容易: 基本上,只需创建一个新文件,它是UITableViewCell的子类。 添加标签并合成它们:
// in the .h file
@property (nonatomic, weak) IBOutlet UILabel *textLabel;
@property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
// in the .m file
@synthesize textLabel, detailTextLabel;
在StoryBoard中,将您的类设置为单元格的类,将样式设置为“ Custom”,然后在单元格中添加两个标签以使其完全符合您的期望(我使它们看起来与默认设置相同:http:/ /cl.ly/J7z3) 最重要的部分是确保将标签连接到单元 您需要在“单元格”中按住Control键单击并单击“文档”大纲中的标签。这是其外观的图片:http://cl.ly/J7BP 这段youtube视频帮助我理解了如何创建自定义单元格,动态单元格和静态单元格:http://www.youtube.com/watch?v=fnzkcV_XUw8 一旦这样做,就应该准备就绪。祝好运!     
当尝试在UITableView中使用\“ Right Detail \”时,我遇到了类似的问题;正确的细节内置标题标签正在破坏我的字幕标签。 最终,我放弃了“ Right Detail”,转而使用了自己的自定义样式(使用swift和autolayout): 我创建了自己的简单类,该类继承自UITableViewCell:
class TransactionCell: UITableViewCell
{

}
我通过在“表格视图单元”菜单上将“样式”字段设置为“自定义”,并在“类”字段中添加“ TransactionCell”,来设置我的原型单元使用该自定义类\“自定义类别\”菜单中。在情节提要中选择原型单元时,这些菜单可用。 我在原型单元格中添加了两个标签,然后通过右键单击将它们从我的标签拖到我的班级上,将它们连接到我的自定义班级(奇怪的是,我必须先清理构建,然后才能执行此操作):
class TransactionCell: UITableViewCell{

    @IBOutlet weak var detailsLabel: UILabel!

    @IBOutlet weak var amountLabel: UILabel!

}
我利用swift的自动布局功能在标签上添加了新的约束(您将需要设置这些约束以匹配自己的要求;如果遇到困难,请参阅自动布局教程) ...并在相应的“标签”菜单中设置“线”和“换行符”字段,以使标签之间的间距均匀,这样我的详细信息标签可以弯曲成多行。 它为我工作,使我能够灵活地在UITableView中以每个单元格快速地显示不同数量的多行,同时格式化自动换行使其看起来不错甚至均匀,就像我期望的“ Right Detail”一样自动执行。     
调整视图框架:textLabel
 CGRect aFrame = cell.textLabel.frame;
 aFrame.size.width = 100;  // for example
 cell.textLabel.frame = aFrame;
    
更新了Gosoftworks Development的答案。 迅捷3
class BaseTableViewCell: UITableViewCell {
    override func layoutSubviews() {
        super.layoutSubviews()

        guard let tl = textLabel, let dl = detailTextLabel else { return }
        if (tl.frame.maxX > dl.frame.minX) {
             tl.frame.size.width = dl.frame.minX - tl.frame.minX - 5        
        }
    }
}
    
支持最新iOS(12)的Swift版本:
override func layoutSubviews() {
    super.layoutSubviews()
    guard let detailWidth = detailTextLabel?.intrinsicContentSize.width, var detailFrame = detailTextLabel?.frame else {
        return
    }

    let padding = layoutMargins.right
    if (detailFrame.size.width <= detailWidth) && (detailWidth > 0) {
        detailFrame.size.width = detailWidth
        detailFrame.origin.x = frame.size.width - detailWidth - padding
        detailTextLabel?.frame = detailFrame

        var textLabelFrame = textLabel!.frame
        textLabelFrame.size.width = detailFrame.origin.x - textLabelFrame.origin.x - padding
        textLabel?.frame = textLabelFrame
    }
}
    
创建一个带有UILabel的自定义UITableViewCell,您可以根据需要控制它,或者截断分配给基类textLabel的文本以适合您的空间。 这不是完美的方法,但是我在使用NSString类别的自定义单元格过大的地方(例如,当唯一的问题是放入文本时)使用了文本截断技术,该方法类似于以下方法:
- (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font
{
    NSString *result = [NSString stringWithString:self];

    while ([result sizeWithFont:font].width > width)
    {
        result = [result stringByReplacingOccurrencesOfString:@\"...\" withString:[NSString string]];

        result = [[result substringToIndex:([result length] - 1)] stringByAppendingString:@\"...\"];
    }

    return result;
}
可能不是“优化”,但适用于简单的情况。     
1:设置换行模式
textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
第二个:设置所需的texLabel框架宽度(例如200)
CGRect textFrame = self.textLabel.frame;
CGRect newTextFrame = CGRectMake(textFrame.origin.x, textFrame.origin.y, 200, textFrame.size.height);
self.textLabel.frame = newTextFrame;
    
有用!!但是我只是更改了克里斯托弗·金的代码:
- (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font :(NSString*) result
{

    while ([result sizeWithFont:font].width > width)
    {
        result = [result stringByReplacingOccurrencesOfString:@\"...\" withString:[NSString string]];

        result = [[result substringToIndex:([result length] - 1)] stringByAppendingString:@\"...\"];
    }

    return result;
}
和用法:
NSString* text = @\"bla bla bla some long text bla bla\";
text = [self stringByTruncatingToWidth:cell.frame.size.width-70.0 withFont:[UIFont systemFontOfSize:17] :text];
cell.textLabel.text = text;
    
我为此苦苦挣扎,找到了一个非常简单的答案。我的
textLabel
位于左侧,会向右推出ѭ22sometimes至有时根本看不到的位置。 我的解决方案是将
Table View Cell
style
Left Detail
Right Detail
更改为
Subtitle
。如果您不介意
detailText
在下方而不是在右侧或左侧,则此解决方案有效。 如果您对行的高度有疑问,可以使用
viewDidLoad
中的以下代码进行调整。
    self.tableView.estimatedRowHeight = 500 // set this as high as you might need, although I haven\'t tested alternatives
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()
    

要回复问题请先登录注册