UITextView in TableCell, How to set the correct width for a generic application

I am using a UITextView inside a tableView cell to 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 this is not true when launched on the iPad.

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

but it always returns 320.0 for iPhone and iPad

Also on the iPad, when in landscape mode, the cell width should not be larger?

Theo

+6
iphone uitableview ipad uitextview
source share
2 answers

Ideally, you should create a custom UITableViewCell and adjust the sizes / positions of your controls in layoutSubviews .

If you are going to add a control to tableView:cellForRowAtIndexPath: you can get the width from tableView itself:

 UITextField *textName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width-50, 24)]; 
+2
source share
  • An iPad cell changes when it is added to the table after your function returns. If you want the text field to be changed using a cell, you can do something like textName.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight textName.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight .
  • You must add custom views in the contentView (ie [cell.contentView addSubview:textName] ). Content browsing is automatically compressed to, among other things, work with edit mode.

The UITableViewCell subclassification went a little too far if you just want to customize the layout - it seems to me that automatic resizing is faster than manually sizing with layoutSubviews.

+1
source share

All Articles