0

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

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

我以这种方式尝试过:

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

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

结果你能在这里看到吗:-(格式问题

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

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

    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 :-( 日志输出正常

4

2 回答 2

2

问题是您使用的是可变宽度字体。字体中的每个字形可能有不同的宽度。因此,您不能只将自动填充到 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;
}
于 2012-08-25T20:10:39.973 回答
0

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

于 2012-08-25T19:55:25.213 回答