无法缩进UITableViewCell子类 [英] Can't indent UITableViewCell subclass

查看:134
本文介绍了无法缩进UITableViewCell子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将UITableViewCell子类化为创建一个带有按钮和2个标签的自定义单元格。使用Dave Mark的Beginning iPhone Development中概述的模式从xib加载单元定义。以下是基本代码:

I have subclassed UITableViewCell to create a custom cell with a button and 2 labels. The cell definition is loaded from a xib using the pattern outlined in Dave Mark's Beginning iPhone Development. Here's the essential code:

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MarketItemCustomCell" owner:self options:nil];

    for (id oneObject in nib)
    {
        if ([oneObject isKindOfClass:[MarketItemCustomCell class]])
        {
            cell = (MarketItemCustomCell *)oneObject;
            break;
        }
    }

标签和按钮按预期显示,但缩进级别不受尊重。我已经实现了如下所示的indentationLevelForRowAtIndexPath,但是单元格仍然一直向左对齐。

The labels and button display as expected but the indentation level is not respected. I have implemented indentationLevelForRowAtIndexPath like below, but the cell is still aligned all the way to the left.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        <snip/>
        return 5;
    }

请注意,当我不使用自定义单元格时,缩进级别正常工作。

Note that the indentation level works fine when I'm not using a custom cell.

任何提示?

推荐答案

iOS更新8:以下代码由于某种原因不再有效

Update for iOS 8: The code below no longer works for some reason

iOS 8的替代建议:

考虑创建一个AutoLayout约束来表示缩进左边距

Consider creating an AutoLayout constraint to represent the indenting left margin

在运行时,修改此约束以更改缩进级别

At runtime, modify this constraint to change the indent level

如果您有自定义UITableViewCell子类,则此子类可以将IBOutlet映射到约束。 (不幸的是Apple没有提供findConstraintByTag / Id函数)

If you have a custom UITableViewCell subclass, this subclass can have an IBOutlet mapped to the constraint. (Unfortunately Apple did not provide a findConstraintByTag/Id function)

可以修改NSLayoutConstraint的具有讽刺意义的常量属性来更改缩进级别。

The ironically named "constant" property of the NSLayoutConstraint can be modified to change the indent level.

对于iOS 7,这有效(但不应再推荐)

基于vodkhang的建议,我在我的UITableViewCell子类中实现了以下解决方案。不确定这是否是最佳解决方案,但似乎工作正常。

Based on vodkhang's suggestion, I implemented the following solution in my UITableViewCell sublass. Not sure if this is the best solution but it appears to work fine.

- (void)layoutSubviews
{
    [super layoutSubviews];

    float indentPoints = self.indentationLevel * self.indentationWidth;

    self.contentView.frame = CGRectMake(indentPoints,
                                          self.contentView.frame.origin.y,
                                          self.contentView.frame.size.width - indentPoints, 
                                          self.contentView.frame.size.height);
}

这篇关于无法缩进UITableViewCell子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆