TableView单元格中TextView的可变高度 [英] Variable Height of TextView In TableView Cell

查看:102
本文介绍了TableView单元格中TextView的可变高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a basic UITableView, that I fill with a web service online, but I can't find a way to set the height of my cells (dynamic numbers of cells) according to the height of my textView.

以下是我填充单元格的方式:

Here's how I fill my cell :

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

I tried this but get an error of 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;
}


推荐答案

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

In following method you can change height of your cell according to your 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。

Also in cellForRowAtIndexPath method calculate width of string in same manner and then give frame of width of yourtextView accordingly. After that add your textview on cell's contentView.

- (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;
}

这篇关于TableView单元格中TextView的可变高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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