uitableviewcell textlabel太长,推出detailtextlabel看不见

luc*_*cas 28 iphone uitableview textlabel

当我使用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;
}
Run Code Online (Sandbox Code Playgroud)

所以我的货币名称在某些条目上会很长.

小智 15

对我来说最简单的是继承UITableViewCell并覆盖layoutSubviews.

无法找到一种可靠的方法来仅仅从标签框架计算位置,因此只需硬件编码附件宽度,在这种情况下,UITableViewCellStyleValue1单元格具有UITableViewCellAccessoryDisclosureIndicator附件类型.

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)


c0m*_*ing 6

@Jhaliya @lucas

cell.textLabel.numberOfLines = 3; // set the numberOfLines
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
Run Code Online (Sandbox Code Playgroud)

看到这里:自定义UITableViewCell.无法应用UILineBreakModeTailTruncation

  • 它就像一个魅力.只需将`UILineBreakModeTailTruncation`替换为`NSLineBreakByTruncatingTail`(自iOS6起不推荐使用). (2认同)

Jha*_*iya -7

使用 UILabellineBreakMode属性将文本限制在宽度范围内UILabel

@property(nonatomic) UILineBreakMode lineBreakMode
Run Code Online (Sandbox Code Playgroud)

如下使用它。

myLabel.lineBreakMode = UILineBreakModeTailTruncation;
Run Code Online (Sandbox Code Playgroud)

以下是可与 一起使用的值列表 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;
Run Code Online (Sandbox Code Playgroud)