UITableViewCell中的字幕对齐

aLF*_*RSi 3 cocoa-touch uitableview uilabel

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

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

这是我的代码

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

任何的想法 ?

sel*_*elf 5

确定如此子类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);
}
Run Code Online (Sandbox Code Playgroud)

这些只是示例值,因此您可以获得这个想法.