【问题标题】:align part of text left and part right in Xcode在Xcode中将部分文本左对齐和右对齐
【发布时间】:2012-11-26 20:01:45
【问题描述】:

我正在用营养成分填充 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;

}

【问题讨论】:

    标签: ios xcode uitableview alignment


    【解决方案1】:

    您也可以使用 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;
        }
    

    【讨论】:

    • 我使用了这段代码,但现在我只看到了 cell.textlabel.text 的内容
    • 这是我现在使用的代码 - 这应该可以工作(包含在上面的编辑问题中)吗?
    • 是不是和你的框架设置代码有关?您不需要这样做 - 因为 UITableViewCell 会自动将一个左对齐和一个右对齐 - 尝试将其注释掉,看看它是否有效:) 我在一个新项目上测试了我的代码,它应该如何工作。
    【解决方案2】:

    你可以使用下面的代码,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }
    
        cell.textLabel.text = @"Left";
        cell.detailTextLabel.text = @"Right";
    
        return cell;
    }
    

    如果您想在单元格中使用多个自定义标签(两个以上),您也可以这样做并将其添加为cell.contentView 的子视图并使用textAlignment 属性对齐。您可以设置这些标签的框架以显示在适当的位置。

    在这种情况下,你需要这样做

    myLabel1.textAlignment = NSTextAlignmentLeft;
    myLabel2.textAlignment = NSTextAlignmentRight;
    

    【讨论】:

    • @maximum411,为了设置框架,你可以试试"cell.textLabel.frame = CGRectMake(..."。对其他标签也这样做。
    • 嗯...这似乎没有改变任何东西。
    • @maximum411, 你是如何设置框架的?你设置的框架是什么?您是否尝试减小左侧标签的宽度并检查?如果没有,您可以创建一个新标签并添加到单元格上。但这绝对应该有效。如果可能,请发布问题的屏幕截图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2023-03-31
    • 2012-02-24
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多