【问题标题】:Expanding UITableViewcell Dynamically when enter text on UITextView [duplicate]在 UITextView 上输入文本时动态扩展 UITableViewcell [重复]
【发布时间】:2013-07-03 09:06:56
【问题描述】:

我在UITableview 的所有单元格中都有一个UITextView。文本视图的高度是动态增加的。随着文本视图大小的增加,我想增加该单元格的高度。 我写的是

    -(BOOL)textView:(UITextView *)textView1 shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {

        NSLog(@"textTag%d",textView1.tag);

        NSString *stringToSave = [textView1.text stringByReplacingCharactersInRange:range withString:text];
        NSLog(@"Save me: %@", stringToSave);

     CGFrame*frame =textView1.frame;
        frame.size.height = textView1.contentSize.height;
        textView1.frame = frame;

//这里我需要增加特定单元格的高度。

if([text isEqualToString:@"\n"])
    {
        [textView1 resignFirstResponder];
    }
    return  YES;
}

【问题讨论】:

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

标签: ios objective-c cocoa-touch


【解决方案1】:

插入这些行:

//class variable, type float
newHeight = textView1.contentSize.height;
//class variable, type int
cellIndex = /*set index of your cell for which you want extended height*/
[yourTable reloadData];

以下几行:

CGFrame*frame =textView1.frame;
        frame.size.height = textView1.contentSize.height;
        textView1.frame = frame;

在 heightForRowAtIndexPath 方法中

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == cellIndex)
    {
        return newHeight;
    }
    return regularHeigh;
}

【讨论】:

  • 当我写这些方法时它显示错误
  • 什么错误?请发帖。
  • 如果 ([self tableView:idationFlowTbl heightForRowAtIndexPath:0]) { return 100; 是否正确[idationFlowTbl beginUpdates]; [idationFlowTbl endUpdates]; }
  • 这是一个回调方法,你不应该直接调用它。但即使你这样做,它也不会做任何事情。
  • 好的,我怎么调用那个方法
【解决方案2】:

将名为HPGrowingTextView 的自定义不断增长的TextView 添加到您的单元格。 然后按照问题所示改变单元格的高度

Change the UITableViewCell Height According to Amount of Text

【讨论】:

    【解决方案3】:

    我认为您可以在NSMutableArray 中映射单元格的高度。 该数组将包含您拥有的标准高度。

    您可以使用indexPath.row 标记UITextView,以便知道要从数组中加载哪个项目。

    您可以根据文本视图的高度覆盖数组中的值。

    然后在你的

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     return [[mycellsizearray objectAtIndex:indexPath.row]floatValue];
    }
    

    现在你应该有符合你需要的高度了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多