Ipad 如何在不使用常量的情况下获得分组的UITableViewCell内容区域宽度

Ipad 如何在不使用常量的情况下获得分组的UITableViewCell内容区域宽度,ipad,ios,Ipad,Ios,我正在为iPad应用程序创建一个表视图控制器。此视图可能会全屏显示或在模式视图中显示,因此每次显示时大小可能不同。理想情况下,我希望使我的代码具有足够的通用性,无论其显示大小如何,都能正常工作。(作为一项学术练习,我也希望该代码也能在iPhone上使用——但这并不是这里的真正要求。) 我使用的是分组表视图样式。我想在单元格的视图中嵌入UITextField。我可以将文本字段放入单元格OK(使用cell.AddSubview),但是当我使用分组样式时,文本字段位于表格的最左侧,而不是它应该位于白色

我正在为iPad应用程序创建一个表视图控制器。此视图可能会全屏显示或在模式视图中显示,因此每次显示时大小可能不同。理想情况下,我希望使我的代码具有足够的通用性,无论其显示大小如何,都能正常工作。(作为一项学术练习,我也希望该代码也能在iPhone上使用——但这并不是这里的真正要求。)

我使用的是分组表视图样式。我想在单元格的视图中嵌入UITextField。我可以将文本字段放入单元格OK(使用cell.AddSubview),但是当我使用分组样式时,文本字段位于表格的最左侧,而不是它应该位于白色区域的位置

我环顾四周(例如UICatalog示例和这里的答案),这个问题的所有解决方案似乎都涉及到对边界区域的恒定x偏移量进行硬编码。这个x偏移在iPad上约为35像素,但在iPhone上约为20像素

在我看来,应该有更好的方法来做这件事,但我还没有找到。我试过查看矩形cell.Bounds、cell.Frame、cell.ContentView.Bounds和cell.ContentView.Frame——它们都没有分组单元格的“实际”内容区域

是否有人有其他建议,或者我需要硬编码该值


谢谢

我也一直在寻找类似的答案

我还没有找到任何“好”的答案,但这段对话确实揭示了为什么调用contentView的帧不会返回实际帧减去附件的专用区域和圆角填充:


我接受了Barrett的建议,只是用硬编码的值相应地调整了框架,但如果您找到更好的答案,我会非常感兴趣。

将任何
UIViews
添加到
单元格。contentView
并为此视图设置
autoResizeMask
属性

下面是使用
UILabel
UITextField
创建
cell
的示例:

// custom cell implementation
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    if ( (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) ) 
    {
        self.label = [[[UILabel alloc] init] autorelease];
        self.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        self.label.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.label];

        self.textField = [[[UITextField alloc] init] autorelease];

        //this will make our textField resized properly 
        self.textField.autoresizingMask =  UIViewAutoresizingFlexibleWidth;

        self.textField.borderStyle = UITextBorderStyleNone;
        self.textField.backgroundColor = [UIColor clearColor];
        self.textField.textColor = [UIColor darkGrayColor]; //text color
        self.textField.font = [UIFont systemFontOfSize:17.0];  //font size
        self.textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

        self.textField.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
        self.textField.returnKeyType = UIReturnKeyNext;  // type of the return key

        self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;   // has a clear 'x' button to the right

        [self.contentView addSubview:self.textField];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    return self;
}
在数据源方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
诸如此类

            CustomCell* c = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (c == nil) 
        {
            c = [[[CellWithPass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease];
            c.textField.delegate = self;
        }
        //in this method cell and cell subviews are not resized, so
        //we are setting subviews frames here for cell with frame {{0,0},{320,44}}
        c.label.text = @"label";
        float w = [c.label.text sizeWithFont:c.label.font].width;
        c.label.frame = CGRectMake(10, 0, w, 44);
        c.textField.frame = CGRectMake(w + 10, 12, c.contentView.frame.size.width - w - 20, 20);
        return c;

您希望将文本字段添加到单元格的
contentView
,而不是直接添加到单元格中。如果这是错误的行为,我们需要看到一些代码。在nib中设置文本字段时会发生什么?