0

私はcell-subtitleのフォーマットの問題を抱えています。セルのサブタイトルに表示したい文字列と数字があります

文字列は左揃え、数字は右揃えにする必要があります

私はそのようにそれを試しました:

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

問題は、可変幅のフォントを使用していることです。フォント内の各グリフの幅は異なる場合があります。したがって、「llll」の後に36個のスペースが続くのは、「MMMM」の後に36個のスペースが続くのとは(画面上で)異なる幅であるため、autorを40文字までパディングすることはできません。

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 に答える