【问题标题】:format at cell.detailTextLabel.text格式为 cell.detailTextLabel.text
【发布时间】:2012-08-25 19:50:07
【问题描述】:

我在单元格字幕中有格式问题。 我想在单元格的副标题中显示一个字符串和一个数字

字符串左对齐,数字右对齐

我是这样试的:

rangeAutor = [rangeAutor stringByPaddingToLength:40-[rangeAutor length] withString:@" " startingAtIndex:0];

NSString *subTitle = [NSString stringWithFormat:@"by %@ %1.2f km", rangeAutor, rangeDistance];
cell.detailTextLabel.text = subTitle;

你可以在这里看到结果吗 :-( format problem

您对我有解决方案的想法吗?

由于我只有 7 个声誉,我无法在 cmets 中写下我的答案 :-( 所以我试着在这里做:

    static NSString *const kIdentifier = @"SelectRange";
static NSInteger const kDistanceLabelTag = 1;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
UILabel *distanceLabel;
if (cell) {
    distanceLabel = (UILabel *)[cell.contentView viewWithTag:kDistanceLabelTag];
} else {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kIdentifier];
    CGRect cellBounds = cell.bounds;
    static CGFloat const kLabelHeight = 20.0f;
    distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, cellBounds.size.height - kLabelHeight, cellBounds.size.width, kLabelHeight)];
    distanceLabel.tag = kDistanceLabelTag;
    distanceLabel.font = cell.textLabel.font;
    distanceLabel.textColor = cell.textLabel.textColor;
    distanceLabel.textAlignment = UITextAlignmentRight;
    distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [cell.contentView addSubview:distanceLabel];
}

UIImage *icon = [UIImage imageNamed:iconName];;
cell.imageView.image = icon;
cell.textLabel.text = rangeName;
cell.detailTextLabel.text = [@"by " stringByAppendingString:rangeAutor];
distanceLabel.text = [NSString stringWithFormat:@"%1.2f km", rangeDistance];
NSLog(@"RangeDistance: %1.2f", rangeDistance);

return cell;

抢劫梅奥夫

我只改变了一些小东西 但现在我在单元格中看不到 rangeDistance :-( 日志输出正常

【问题讨论】:

    标签: objective-c format subtitle


    【解决方案1】:

    问题是您使用的是可变宽度字体。字体中的每个字形可能有不同的宽度。所以你不能只将自动填充到 40 个字符,因为“llll”后跟 36 个空格的宽度(在屏幕上)与“MMMM”后跟 36 个空格的宽度不同。

    只需给您的cell 另一个标签以保持距离。我假设您帖子中的代码来自您的 tableView:cellForRowAtIndexPath: 方法。您需要执行以下操作:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *const kIdentifier = @"Cell";
        static NSInteger const kDistanceLabelTag = 1;
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
        UILabel *distanceLabel;
        if (cell) {
            distanceLabel = (UILabel *)[cell.contentView viewWithTag:kDistanceLabelTag];
        } else {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kIdentifier];
            CGRect cellBounds = cell.bounds;
            static CGFloat const kLabelHeight = 20.0f;
            distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, cellBounds.size.height - kLabelHeight, cellBounds.size.width, kLabelHeight)];
            distanceLabel.tag = kDistanceLabelTag;
            distanceLabel.font = cell.textLabel.font;
            distanceLabel.textColor = cell.textLabel.textColor;
            distanceLabel.textAlignment = UITextAlignmentRight;
            distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
            [cell.contentView addSubview:distanceLabel];
        }
    
        cell.imageView.image = rangeImage;
        cell.textLabel.text = rangeName;
        cell.detailTextLabel.text = [@"by " stringByAppendingString:rangeAutor];
        distanceLabel.text = [NSString stringWithFormat:@"%1.2f km", rangeDistance];
    
        return cell;
    }
    

    【讨论】:

    • 我不确定我是否解决了您的问题。如果我还没有解决你的问题,你应该取消选中我的答案。
    • 对不起,我尝试同时学习objective-c、英语和这个论坛;-) 问题没有解决,现在我看不到rangeDistance(我编辑问题并添加我的代码)
    • 抱歉回答晚了。工作正常。一位朋友提示我删除故事板中的单元格。
    【解决方案2】:

    尝试使用常量值而不是40-[rangeAutor length]

    【讨论】:

    • 如果我将该行更改为: rangeAutor = [rangeAutor stringByPaddingToLength:30 withString:@" " startingAtIndex:0];它看起来像:link
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2015-07-18
    • 2018-12-23
    • 2016-06-14
    相关资源
    最近更新 更多