我在单元格字幕中有格式问题。我有一个字符串和一个数字,我想在单元格的副标题中显示
字符串应该左对齐,数字右对齐
我以这种方式尝试过:
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 :-( 日志输出正常