6

i am using a UITextView inside a tableView cell in order edit text.

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

UITextField *textName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 24)];

[cell addSubview:textName];
[textName release];

}

This works, but when running it on the iPad it isn't correct.

I've tried to determine the width of cell using cell.contentView.frame.size.width

but this always returns 320.0 for both iPhone and iPad

Also on the iPad when in landscape mode shouldn't the width of cell be bigger?

Teo

TomSwift
  • 39,369
  • 12
  • 121
  • 149
teo
  • 269
  • 1
  • 5
  • 11

2 Answers2

2

Ideally you'd create a custom UITableViewCell and adjust your control sizes/positions in layoutSubviews.

If you're going to add the control in tableView:cellForRowAtIndexPath:, then you can get the width from the tableView itself:

UITextField *textName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width-50, 24)];
TomSwift
  • 39,369
  • 12
  • 121
  • 149
1
  1. The iPad cell is resized when it's added to the table, after your function returns. If you want the text field to resize with the cell, you can do something like textName.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight.
  2. You should add custom views to contentView (i.e. [cell.contentView addSubview:textName]). The content view automatically shrinks to handle editing mode, among other things.

Subclassing UITableViewCell is a bit overkill if you just want to tweak layout — it's my impression that auto-resizing is faster than manual sizing using layoutSubviews.

tc.
  • 33,468
  • 5
  • 78
  • 96