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开发者_如何学Python mode shouldn't the width of cell be bigger?
Teo
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)];
- 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
. - 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.
精彩评论