微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UITableViewCell textLabel偏移量

如何解决UITableViewCell textLabel偏移量

| 我想在UITableViewCell中添加文本,使其稍微偏右。换句话说,我想要一个x偏移。这甚至可能吗?我是否仅因为这个原因就必须创建一个自定义单元格?     

解决方法

您可以尝试以下方法:
[cell setIndentationLevel:SOME_NUMBER];
[cell setIndentationWidth:SOME_OTHER_NUMBER];
    ,您可以使用
cell.indentationLevel
,如果需要的话可以使用
cell.indentationWidth
,而不是自定义UITableViewCell。     ,一个简单的解决方案是您可以更改textLabel的框架。
CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += xOffset;
textLabelFrame.size.width -= xOffset;
cell.textLabel.frame = textLabelFrame;
我还通过创建一个自定义UILabel来做到这一点,该UILabel支持类似于UIButton的edgeInsets。这是一个更好的解决方案,因为您可以将标签布局为正确的尺寸,但是如果您有简单的需求,上述方法也可以使用。 [编辑1/2:带有CGRect的固定错字] [编辑3:固定错字设置修改框] [编辑4:需要一个简单的子类] Mea culpa。您可以在
tableView:cellForRowAtIndexPath
中做到这一点,这是错误的。 UITableViewCell布局在tableView委托/数据源有机会自定义单元格之后发生。我已经测试了以下实现,它可以正常工作。 如我上面所述,但创建一个(简单的)ѭ5子类,在
layoutSubviews
中添加
xOffset
。如果这样做,还可以添加可以在
tableView:cellForRowAtIndexPath:
中设置的
xOffset
属性。
@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
建议将自定义“ 11”添加到“ 12”的解决方案也是一个很好的解决方案。我看到您的评论说它掩盖了内置的
textLabel
,但这就是重点。您将不再使用内置标签,而应使用自定义标签。     ,我不是要投票,而是想展示@iPhone Monster“应该”提供的代码是什么样的。他的解决方案是有效的选择。如果像他一样在
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;
}
    ,您可以使用自动版式来做到这一点:
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}]];
或使用Parus lib:
[cell.contentView addConstraints:(PVVFL(@\"H:|-(indentation)-[textLabel]-(indentation)-|\")
                                  .withViews(@{@\"textLabel\": textLabel})
                                  .metrics(@{@\"indentation\": @35}).asArray)];
    ,其他方式:
    -(NSInteger)tableView:(UITableView *)tableView 
                indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return intOffset;  // but is like \'\\t\',not points
    }
    ,还有另一种不是很漂亮和聪明的方法:)Label在TableViewCell中的位置取决于分配给cell.imageView.image属性的图像大小。因此,如果您希望文本向右移动,则可以向单元格中的图像添加几个空白像素列。     ,ѭ19够了。它可以使文本与中心对齐,为您创造空间     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。