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

Joh*_*ohn 0 ipad ios

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

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

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

在我看来应该有一个更好的方法来做到这一点,但我还没有找到它.我已经尝试过查看矩形cell.Bounds,cell.Frame,cell.ContentView.Bounds和cell.ContentView.Frame - 它们都没有分组单元格的"实际"内容区域.

有没有人有其他建议,还是我需要硬编码价值?

谢谢

小智 6

添加任何UIViewscell.contentView并设置autoResizeMask该视图属性

这是cell使用UILabel和创建的示例UITextField:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

并在dataSource方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

类似的东西

            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;
Run Code Online (Sandbox Code Playgroud)