通过 UITextView 内容大小调整 UITableView 单元格 [英] Resizing UITableView cell by UITextView contents size

查看:25
本文介绍了通过 UITextView 内容大小调整 UITableView 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助.我有一个带有自定义 UITableViewCell 的 UITableView.在单元格中,我有一个 UITextView.我需要做的是,当用户将一些数据输入到 UITextView 中时.UITextView 将从 UITextView 的内容调整大小.我在 - (void)textViewDidChange:(UITextView *)textView 中意识到它但是现在我还需要调整单元格的大小,如果 UITextView 的内容大小会大于当前单元格.

I need some help. I have a UITableView with custom UITableViewCell. In the Cell I have one UITextView. I need to do, when the user will type some data into UITextView. UITextView will resize from content of UITextView. I realize it in - (void)textViewDidChange:(UITextView *)textView But now I need to resize the cell also, if content size of UITextView will bigger then the current Cell.

问题是如何通过 UITextView 内容大小调整 UITableView Cell 的大小.

The question is how to resize UITableView Cell by UITextView contents size.

我的代码如下.

@class TableCell;
@interface RootViewController : UITableViewController{
UITableView*tableViewTest;
TableCell *tableCell;
}
@property (nonatomic,retain)IBOutlet TableCell *tableCell;
@property (nonatomic,retain)IBOutlet UITableView*tableViewTest;
@end



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}

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

    static NSString *CellIdentifier = @"Cell";

    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
          [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
    cell=tableCell;
    self.tableCell=nil;
}
[cell setTag:indexPath.row+1];

return cell;
 }

- (void)textViewDidChange:(UITextView *)textView
{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
NSLog(@"Change=%f",textView.contentSize.height);    
}




@interface TableCell : UITableViewCell <UITextViewDelegate>{
UITextView *textViewTest;
}
@property (nonatomic,retain) IBOutlet UITextView *textViewTest;

感谢帮助

推荐答案

在 UITableViewDelegate 中你可以通过函数调整单元格高度

in UITableViewDelegate you can resize your cell height by function

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do your stuff here like resize your cell height 
}

然后为了反映视图的变化,你需要重新加载表

and after that for reflecting the change in view you need to reload table by

[self.table reloadData];

如果您的表格中有部分,那么您不需要重新加载整个表格并避免在视图中闪烁,您只需重新加载表格的特定部分,您可以通过该单元格的索引路径对其执行高度更改,您也可以使用其他一些在这里找到所需单元格的方法是我找到需要重新加载的单元格的风格

and if you have section in your table then you need not to reload whole table and avoid flickering in view you just reload that specific section of table for which you perform change in height by indexpath of that cell you can also use some other way to find the desired cell here is my style to find the cell which i need to reload

NSIndexPath *ip = [self.table indexPathForCell:cell];
[self.table reloadSections:[NSIndexSet indexSetWithIndex:ip.section] withRowAnimation:UITableViewRowAnimationAutomatic];

这篇关于通过 UITextView 内容大小调整 UITableView 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆