【问题标题】:Resizing UITableView cell by UITextView contents size通过 UITextView 内容大小调整 UITableView 单元格的大小
【发布时间】:2011-07-04 16:03:18
【问题描述】:

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

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

下面是我的代码。

@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;

感谢您的帮助

【问题讨论】:

    标签: ios uitableview uitextview


    【解决方案1】:

    不要调用 reloadData,而是使用 [table beginUpdates] 的东西

    【讨论】:

      【解决方案2】:

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

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

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

      [self.table reloadData];
      

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

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

      【讨论】:

        【解决方案3】:

        老问题,但如果仍在寻找这个问题的答案,一些自动布局魔法可能会对某人有所帮助。

        如果一切设置正确(自动布局约束),您不必自己计算任何内容。

        您所要做的就是禁用 UITextView 的启用滚动属性,然后在 textViewDidChange 上调用 tableView.beginUpdates() 和 tableView.endUpdates()。

        有关详细说明,请查看我写的post,其中还包括一个工作示例项目。

        【讨论】:

          【解决方案4】:

          你只能通过实现增加自定义UITableViewCell的高度delegateheightForRowAtIndexPath:

          当单元格的 textView 变得大于单元格高度时,您需要在UITableView 实例上调用reloadData 方法,并且heightForRowAtIndexPath: 函数必须为扩展单元格返回正确的高度。

          【讨论】:

          • 如果我在输入第一个字母后重新加载 textViewDidChange 中的数据,重新加载表格视图并结束输入会话。但这不是问题。我们可以从 textViewDidEndEditing 方法重新加载。 heightForRowAtIndexPath 方法:在应用程序开始加载表格时加载。如何验证空单元格?
          • 您将希望保持每个单元格的高度作为模型的一部分,并且您可能只想调用- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 我会担心重新加载表格视图可能会导致关键要辞职的领域,你必须尝试一下才能看到
          • @bshirley :同意你的观点,但不确定reloadRowsAtIndexPaths: 会导致调用heightForRowAtIndexPath:
          • 它很可能不会,我尝试在编辑和不编辑之间切换时调整单元格的大小,但是此时没有调用 height 方法(实际上,我通常只需要使单元格更大,所以这就是我所做的)
          【解决方案5】:

          UITableViewDelegate 协议定义:

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

          这允许您指定每个单元格的高度。 UITextView 将为每个可见单元格调用数据源。你可以通过调用UITableViewreloadData来触发这个

          【讨论】:

          • 诺兰:再次检查heightForRowAtIndexPath:UITableViewDelegate 的一部分,而不是UITextViewDataSource
          猜你喜欢
          • 2015-10-03
          • 2013-05-26
          • 1970-01-01
          • 1970-01-01
          • 2021-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-05
          相关资源
          最近更新 更多