开发者

How to use UITextView in UITableViewCell with variable height?

开发者 https://www.devze.com 2023-01-20 04:29 出处:网络
I am interested in creating a UITableViewCell with a UITextView inside of it, which adjusts its height (to a certain maximum) as the user types more or less text into the UITextView. I know how to add

I am interested in creating a UITableViewCell with a UITextView inside of it, which adjusts its height (to a certain maximum) as the user types more or less text into the UITextView. I know how to add the UITextView to a custom UITableViewCell--but how might I change the UITableViewCell's he开发者_C百科ight as the user is in the process of typing? Is this possible to do?


As far as I know the only way to set the height of a table cell is through the delegate heightForRowAtIndexPath: method. To invoke this method you can call reloadData on the table but I'm not sure what effect that would have on the UITextView you are editing. Worth a try though.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 100;
}

To calculate the return value you would need to work out the height based on the current input and expand the text view at the same time. NSString has some helpful methods for this such as sizeWithFont:constrainedToSize:lineBreakMode:. It will work out a CGSize you can use to make your CGRect etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号