【问题标题】:Variable Height of TextView In TableView CellTableView单元格中TextView的可变高度
【发布时间】:2013-02-17 14:29:26
【问题描述】:

我有一个基本的UITableView,我在网上填写了一个网络服务,但是我找不到根据我的textView 的高度来设置我的单元格高度(动态单元格数)的方法.

这是我填充单元格的方式:

UITextView *textView = (UITextView *)[cell viewWithTag:106];
textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:@"texte"];

在我的cellForRow 方法中:

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

我试过了,但得到了EXC_BAD_ACCESS的错误

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PerleCell"];
    UITextView *textView = (UITextView *)[cell viewWithTag:106];
    textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:@"texte"];
    CGFloat *heightCell =  (CGFloat*)textView.text.length;
    return *heightCell;
}

【问题讨论】:

  • 您好,亲爱的,看看我的代码,如果您遇到任何问题,请回复我。记住在创建从不双端单元格时。始终创建新单元格并根据需要修改该单元格,然后添加返回该单元格。
  • 不使单元格出列是不好的建议,并且会使您的应用程序使用更多的内存。

标签: iphone ios objective-c uitableview


【解决方案1】:

在以下方法中,您可以根据 textView 更改单元格的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // Here check length of your textView
    // and after getting length return that length;
    NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:@"texte"];
    CGSize maximumSize = CGSizeMake(300, 9999);
    UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
    return size.height;
}

同样在 cellForRowAtIndexPath 方法中以相同的方式计算字符串的宽度,然后相应地给出 yourtextView 的宽度框架。之后在单元格的 contentView 上添加你的 textview。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     // Create cell here by using any style and for this you can google some what that how to create cell in UITableView
     // Here check length of your textView
     // and after getting length return that length;
     NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:@"texte"];
     CGSize maximumSize = CGSizeMake(300, 9999);
     UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
     CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
     UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,300,size.height)];
     textView.text = myString;
     [cell.contentView addSubView:textView];
     return cell;
}

【讨论】:

  • 另外,虽然 dequeCell 从不使用重用标识符,因为您的单元格大小不同,会产生问题。我还没有测试过,但我希望这对你有用。
  • 感谢您的回答,但我根据您的建议编辑了我的问题并得到了一个错误:s
  • EXC_BAD_ACCESS 返回
  • 你为什么要在这里访问单元格。只创建 textView 然后计算该 textView 的高度
  • 您关于不重复使用单元格的评论不正确。您应该重用单元格以提高效率,并且所有调整大小都应该在 heightForRowAtIndexPath: 中完成,而不是在 cellForRowAtIndexPath: 中。重用的单元格在重用之前将通过 heightForRowAtIndexPath: 重置其大小,因此这应该不是问题。我不知道这是否有任何区别,但我对这种事情使用多行标签而不是文本视图。
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
相关资源
最近更新 更多