【问题标题】:How to reliably subclass UITableViewCell for grouped UITableView?如何可靠地将 UITableViewCell 子类化为分组的 UITableView?
【发布时间】:2012-03-12 08:41:39
【问题描述】:

在编写UITableViewCell 的自定义子类时,我发现结果对于普通样式的UITableView 的矩形单元格效果很好,但对于分组样式表中的圆形单元格则根本不起作用。

有没有办法可靠地继承 UITableViewCell 来绘制适用于分组样式表的单元格? (不使用 Interface Builder。)

【问题讨论】:

    标签: iphone uitableview subclass


    【解决方案1】:

    答案是否可以像在 UITableViewCell 子类的 layoutSubviews 方法中首先调用 [super layoutSubviews] 一样简单?

    这是我的代码。

    首先我创建UITextField 并将其添加到initWithStyle: 方法中的contentView

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            inputField = [[UITextField alloc] initWithFrame:CGRectZero];
            [self.contentView addSubview:inputField];
            inputField.borderStyle = UITextBorderStyleLine;
            [inputField release];
        }
        return self;
    }
    

    然后在 layoutSubviews 中,我得到了这个:

    -(void)layoutSubviews
    {
        inputField.frame = CGRectMake(5, 5, 100, 20);
    }
    

    使用该代码,文本字段距离屏幕左侧 5px,当然,当处于分组模式时,也就是表格单元格左侧 5px。换句话说,在表格视图单元格之外。不好。

    使用此代码,inputField 被放置在单元格右侧 5px 处,就像我希望的那样:

    -(void)layoutSubviews
    {
        [super layoutSubviews]; // the magic line
        inputField.frame = CGRectMake(5, 5, 100, 20);
    }
    

    不过,我可能完全误解了你遇到的问题!

    埃里克

    【讨论】:

    • 我怎么能错过呢?!!非常感谢你的魔法线!
    【解决方案2】:

    我曾经对UITableViewCell 子类有很多问题,但后来我停止了子类化。 在UITableViewCellcontentView 属性中添加子视图似乎在我遇到的任何情况下都能完成同样的事情,所以我只是在我的UITableViewController 中这样做。

    这是一个有标题和值的例子:

    - (UITableViewCell *)tableView:(UITableView*)tableView 
             cellForRowAtIndexPath: (NSIndexPath*)indexPath 
    {    
        static NSString* CellIdentifier = @"AccountDetailsCell";
        UILabel* mainLabel = nil;
        UILabel* valueLabel = nil;
        const CGFloat kAccountDetailFontSize = 14.0;
    
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
        if ( cell == nil ) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
                                           reuseIdentifier: CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
            mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 10.0, 0.0, 150.0, 44.0 )] autorelease];
            mainLabel.tag = MAINLABEL_TAG;
            mainLabel.font = [UIFont boldSystemFontOfSize: kAccountDetailFontSize];
            mainLabel.textAlignment = UITextAlignmentLeft;
            mainLabel.textColor = [UIColor darkGrayColor];
            mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
            mainLabel.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview: mainLabel];
    
            valueLabel = [[[UILabel alloc] initWithFrame: CGRectMake( 150.0, 0.0, 150.0, 44.0 )] autorelease];
            valueLabel.tag = VALUELABEL_TAG;
            valueLabel.font = [UIFont boldSystemFontOfSize: kAccountDetailFontSize];
            valueLabel.textAlignment = UITextAlignmentRight;
            valueLabel.textColor = [UIColor darkTextColor];
            valueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            valueLabel.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview: valueLabel];
        }
        else
        {
            mainLabel = (UILabel*)[cell.contentView viewWithTag: MAINLABEL_TAG];
            valueLabel = (UILabel*)[cell.contentView viewWithTag: VALUELABEL_TAG];
        }
    
        mainLabel.text = (NSString*)kCellTitles[indexPath.section][indexPath.row];
        valueLabel.text = [self tableView: tableView valueLabelTextForRowAtIndexPath: indexPath];
    
        return cell;
    }
    

    【讨论】:

    • 我发现这种方法不足以满足我的需求。我有一个单元格,它有一个多行标题和多行描述,并根据两个标签中包含的文本自动调整大小。我在 -(void)layoutSubviews 中调整大小并记住首先调用 [super layoutSubviews],但在我看来 self.contentView 直到此调用之后仍未调整大小,因此有时我的单元格最终高度错误。
    • 这样的方法不错,但是如果你想用QuartzCore的东西来做标签的阴影,那么滚动会很慢,你必须自己绘制单元格。
    • 从那以后,我对子类更加满意,因为我确实遇到的许多问题在最近的 SDK 中得到了纠正。我当前的方法涉及一个带有configureWithDataObject: (id)dataObject 方法的子类,然后我将其子类化为我的自定义单元格。我的视图控制器自动选择正确的单元格子类并调用configureWithDataObject:。使开发变得快速而简单(尽管从技术上讲,它的效率不如在静态代码中完成)。
    【解决方案3】:

    我也注意到了这个问题。我的解决方法是让我的表格单元格更小(300 宽度而不是 320)。这不是一个很好的解决方案,但效果很好。

    我不相信您可以在“分组”模式下单独删除表格视图插图。不过我可能是错的!

    【讨论】:

      【解决方案4】:

      你有什么问题?在 drawRect 中,您将获得一个矩形,并且知道您的总大小 - 只需符合该​​空间即可。如果你使用 layoutSubviews,同样的事情。

      【讨论】:

      • 问题是分组模式下的 UITableView 会切断部分单元格(侧面,以显示背景,并且它还会使部分顶部和底部单元格的角变圆)。
      • 经过一些进一步的测试,你是对的,表格视图单元格在drawRect中没有以正确的大小传递。另一种方法是为标准 UITableViewCell 设置自定义 backgroundView,并在那里进行所有绘图。您会得到一个宽度合适的单元格,但您必须自己正确绘制圆角和圆角。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多