format at cell.detailTextLabel.text



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

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

我试过了:

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;

抢劫mayoff

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

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

只需给你的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;
}

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

最新更新