TableView单元格中TextView的可变高度

我有一个基本的UITableView ,我在网上填写一个Web服务,但我找不到一种方法来根据我的textView的高度来设置我的单元格的高度(单元格的dynamic数量)。

以下是我如何填写我的单元格:

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

在下面的方法中,你可以根据你的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方法中,以相同的方式计算string的宽度,然后相应地给出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; }