Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView in TableCell, How to set correct Width for Universal app

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

like image 225
teo Avatar asked Mar 10 '11 18:03

teo


2 Answers

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)];
like image 141
TomSwift Avatar answered Nov 20 '22 15:11

TomSwift


  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.

like image 30
tc. Avatar answered Nov 20 '22 15:11

tc.