【问题标题】:How can I align 2 values Left and Right within the one cell?如何在一个单元格内将 2 个值左对齐和右对齐?
【发布时间】:2014-01-14 06:27:42
【问题描述】:

我需要以下方面的帮助。我希望 p.no 与单元格的左侧对齐,而 p.name 与单元格的右侧对齐。有什么建议吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:NSDateFormatterShortStyle];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", p.no , p.name];


    return cell;
}

【问题讨论】:

  • 创建您自己的自定义单元格,其中包含两个正确对齐的标签。有关创建您自己的自定义单元格的详细信息,请参阅“iOS 表视图编程指南”。
  • 使用“UITableViewCellStyleValue1”而不是“UITableViewCellStyleSubtitle”,然后设置 cell.textLabel.text = p.no 和 cell.detailTextLabel.text = p.name

标签: ios alignment cell


【解决方案1】:

按照以下步骤操作:
1. 将 2 个标签拖放到情节提要中您的原型单元格上您希望它们所在的位置。
2. 在你的项目中创建一个 UITableViewCell 类的子类。
3. 将新创建的子类分配为身份检查器中表格单元格的类。
4. 用 customTableviewcell.h 连接两个标签出口
5.在tableViewController中导入customTableViewCell.h。
6. 现在在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中,像这样替换代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:NSDateFormatterShortStyle];

    customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];
    cell.label1.text = [NSString stringWithFormat:@"%@ , p.no ];

    cell.label2.text = [NSString stringWithFormat:@"%@ , p.name ];

    return cell;
}

如果有任何问题,请告诉我:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2011-04-14
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多