UITableViewCell textLabel偏移量

ahe*_*ang 22 iphone objective-c uitableview

我希望我的UITableViewCell中的文本有点向右.换句话说,我想要一个x偏移量.这甚至可能吗?我是否因此而需要创建自定义单元格?

dem*_*ten 109

你可以使用cell.indentationLevel,如果需要,cell.indentationWidth而不是自定义UITableViewCell.

  • 这是最好的答案,为什么这不是标记为解决方案?这正是这个家伙所要求的. (6认同)

iOS*_*ter 31

你可以试试这个:

[cell setIndentationLevel:SOME_NUMBER];
[cell setIndentationWidth:SOME_OTHER_NUMBER];
Run Code Online (Sandbox Code Playgroud)

  • 您应该将子视图添加到单元格的contentView,而不是直接添加到单元格本身. (9认同)
  • 虽然这个解决方案的想法很好(添加自定义标签),但代码示例并不是很好.首先,@ Mike Weller是正确的,子视图应该添加到`cell.contentView`.其次,新标签应该在`if(cell == nil)`检查中添加,这样你就不会为已经拥有它们的已出列的单元格实例添加标签.在标签上设置标签,以便在获取单元格后检索它,以便设置文本. (9认同)

XJo*_*nes 11

一个简单的解决方案是您可以更改textLabel的框架.

CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += xOffset;
textLabelFrame.size.width -= xOffset;
cell.textLabel.frame = textLabelFrame;
Run Code Online (Sandbox Code Playgroud)

我也是通过创建一个支持类似于UIButton的edgeInsets的自定义UILabel来完成的.这是一个更好的解决方案b/c您可以将标签布局为正确的尺寸,但如果您有简单的需求,上述方法将起作用.

[编辑1/2:修复错字与CGRect]

[编辑3:固定拼写设置修改框架]

[编辑4:需要一个简单的子类]

Mea culpa.我错了,你可以做到这一点tableView:cellForRowAtIndexPath.在tableView委托/数据源有机会自定义单元格之后,UITableViewCell布局发生.我测试了下面的实现,它的工作原理.

按我上面说的,但创建一个(简单)的子类UITableViewCell,增加了xOffsetlayoutSubviews.如果这样做,您还可以添加xOffset可以设置的属性tableView:cellForRowAtIndexPath:.

@implementation XOffsetCell
// assumes property xOffset is defined and synthesized
- (void) layoutSubviews
{
    [super layoutSubviews];
    CGRect textLabelFrame = cell.textLabel.frame;
    textLabelFrame.origin.x += self.xOffset;
    textLabelFrame.size.width -= self.xOffset;
    cell.textLabel.frame = textLabelFrame;
}
@end
Run Code Online (Sandbox Code Playgroud)

该建议增加一个自定义的解决方案UILabelcell.contentView也是一个不错的解决方案.我看到你的评论它掩盖了内置,textLabel但这就是重点.您不再使用内置标签,而是使用自定义标签.


XJo*_*nes 5

我不是为了投票,而是想表明@iPhone怪物提供的代码应该是什么样子.他的解决方案是有效的选择.如果您像往常一样将标签添加到单元格中,if (cell == nil)您将不断向出列单元格添加标签.

- (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] autorelease];
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 150.0f, 20.0f)];
        lbl.tag = OffsetLabelTag; // define this as a constant
        [cell.contentView addSubview:lbl];
        [lbl release];
    }

    UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:OffsetLabelTag];
    [lbl setText:@"test text"];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)


Kos*_*kov 5

您可以使用自动布局做到这一点:

UITableViewCell *cell = [UITableViewCell new];
cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(indentation)-[textLabel]-(indentation)-|"
                                                                         options:0
                                                                         metrics:@{@"indentation": @35}
                                                                           views:@{@"textLabel": cell.textLabel}]];
Run Code Online (Sandbox Code Playgroud)

或使用Parus lib

[cell.contentView addConstraints:(PVVFL(@"H:|-(indentation)-[textLabel]-(indentation)-|")
                                  .withViews(@{@"textLabel": textLabel})
                                  .metrics(@{@"indentation": @35}).asArray)];
Run Code Online (Sandbox Code Playgroud)