0

I'm creating a table view controller for an iPad app. This view will potentially be displayed full-screen or within a modal view, so the size could be different each time it's displayed. I'd ideally like to make my code generic enough to work irrespective of the size it's displayed at. (As an academic exercise I'd also like the code to work on the iPhone too - but that's not really a requirement here.)

I'm using a grouped table view style. I want to embed a UITextField into the cell's view. I can get the text field into the cell OK (using cell.AddSubview), but when I use the grouped style, the text field is at the very left of the table - not where it should be in the white area.

I've looked around (e.g. at the UICatalog sample, and at the answers here) and all of the solutions to this problem seem to involve hard-coding a constant x offset for the border area. This x offset is around 35px on the iPad, but is around 20px on the iPhone.

It seems to me that there should be a better way of doing this, but I've yet to find it. I've tried looking at the rectangles cell.Bounds, cell.Frame, cell.ContentView.Bounds, and cell.ContentView.Frame - none of them have the 'actual' content area of a grouped cell.

Does anyone have another suggestion, or do I need to hard-code the value?

Thanks

John
  • 5,452
  • 8
  • 37
  • 37
  • You want to add the text field to the cell's `contentView`, not the cell directly. If that's misbehaving, we need to see some code. What happens when you set up the text field in a nib? – rgeorge Mar 08 '11 at 05:58

2 Answers2

6

add any UIViews to cell.contentView and set autoResizeMask property for this view

here is the sample of creating cell with UILabel and 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;
}

and in dataSource method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

something like that

            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;
cduck
  • 2,691
  • 6
  • 29
  • 35
mashe
  • 89
  • 1
  • 7
1

I also have been looking for a similar answer.

I am yet to find any kind of "good" answer, but this conversation does kind of shed some light as to why a call to contentView's frame doesn't return the actual frame minus the accessory's dedicated area and rounded edge padding:

UITableViewCell's contentView's width with a given accessory type

I've taken Barrett's advice and just adjusted the frame accordingly with hard coded values, but would be very interested if you find an answer that is better.

Community
  • 1
  • 1
edelaney05
  • 6,822
  • 6
  • 41
  • 65