ios - 在 Xcode 中左对齐部分文本,右对齐部分文本

标签 ios xcode uitableview alignment

我正在用营养成分填充 UITableView 表。每行包括营养素的绝对量以及每日摄入量的百分比。我想将金额对齐到每行的左侧,将百分比每日值对齐到右侧,以便信息看起来更整洁,并且所有值都对齐。有什么办法可以做到这一点?谢谢!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NutritionCell" forIndexPath:indexPath];


    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"NutritionCell"];
    }


    CGRect oldFrame = cell.frame;
    cell.textLabel.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height);
    cell.detailTextLabel.frame = CGRectMake(oldFrame.origin.x + tableView.frame.size.width/2, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height);

    cell.textLabel.text = [factamount objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [percentDV objectAtIndex:indexPath.row];
    return cell;

}

最佳答案

您也可以使用 UITableViewCellStyleValue1 执行此操作。这会自动向单元格添加 2 个标签:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"CELLID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        }

        cell.textLabel.text = @"AMOUNT TEXT";
        cell.detailTextLabel.text = @"PERCENT TEXT";
        return cell;
    }

关于ios - 在 Xcode 中左对齐部分文本,右对齐部分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783470/

相关文章:

iphone - 完全用代码复制 PhotoScroller

ios - 将 UIView 保存为具有透明背景的 png 文件

ios - misaligned_stack_error_是什么意思?

iOS 从另一个类访问 int

iphone - 在后台接收 iOS 通知::本地通知

ios - 快速加载 UITableViewCell 中的 MKMapView

ios - 已达到可用分发证书的最大数量

ios - 如何播放iOS系统声音并取消回调

ios - 来自 API 的数据未显示在我的 UITableView 中

ios - UIimageView 和 Accessory View 在 UITableViewCell 中分开工作