【问题标题】:subtitle alignment in UITableViewCellUITableViewCell 中的字幕对齐
【发布时间】:2012-06-01 13:10:28
【问题描述】:

我正在尝试在单元格中构建带有字幕文本的表格视图

问题是当我尝试将字幕文本的对齐方式设置为右侧时它不起作用,但它适用于正文

这是我的代码

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

    static NSString *CellIdentifier = @"CustomCell";

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

    [[cell textLabel] setTextAlignment:UITextAlignmentRight];
    [[cell detailTextLabel] setTextAlignment:UITextAlignmentRight];

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.detailTextLabel.text = @"test";
    return cell;
}

当我删除字幕代码时,对齐工作正常

有什么想法吗?

【问题讨论】:

    标签: cocoa-touch uitableview uilabel


    【解决方案1】:

    好吧,继承 UITableView Cell 并使用 init 自定义标签。您可以覆盖 layoutSubviews 并将标签向右移动:

    - (void)layoutSubviews {
        [super layoutSubviews];
        self.textLabel.frame = CGRectMake(0.0, 68.0, 80.0, self.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(0.0, 68.0, 120.0, self.frame.size.height);
    }
    

    这些只是示例值,您可以理解。

    【讨论】:

    • @H.aLFaRSi 让我知道,如果我能提供任何帮助:)
    【解决方案2】:

    为什么要初始化单元两次:

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

    您可以创建带有两个标签的自定义单元格并为这些标签提供对齐方式。按照以下步骤操作:

    1.添加UITableViewCell的新文件子类说labelCustomCell。 2.在 labelCustomCell 中创建两个标签,分别是 label1 和 label2。 3.在 initWithStyle 方法中分配这些标签并提供对齐。 4.在 layoutSubViews 方法中为这些标签分配框架。 5.在cellForRowAtIndexPath方法中编写如下代码:

        static NSString *CellIdentifier = @"DataEntryCell";
                    labelCustomCell *cell = (labelCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    if (cell == nil) {
                        cell = [[[labelCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                    }
    cell.label1.text = [array objectAtIndex:indexPath.row];
        cell.label1.font = [UIFont systemFontOfSize:18];
        cell.label2.text = @"test";
    

    别忘了导入 labelCustomCell。

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多