如何在 UITextView 的基础上制作一个带有 UITextView 的 UITableViewCell,动态调整其高度? [英] How to make a UITableViewCell with a UITextView inside, that dynamically adjust its height, on the basis of the UITextView?

查看:28
本文介绍了如何在 UITextView 的基础上制作一个带有 UITextView 的 UITableViewCell,动态调整其高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 tablew 视图,其行为类似于 Apple 的 iPhone 通讯录应用程序:一个带有 uitextview 的 uitableviewcell,这样当我在 uitextview 中写入时,uitextview 会增加其高度,因此 uitableviewcell 会相应地增加动态调整其高度.全网搜索,只找到部分解决方案,缺少示例代码!

I would like to have a tablew view with a behaviour similar to the iPhone Contacts app by Apple: a uitableviewcell with a uitextview inside, so that when I write in the uitextview, the uitextview increases its height, and so accordingly the uitableviewcell dynamically adjusts its height. I searched over the whole web, finding only partial solutions and lack of sample code!

请帮帮我,我很绝望

托尼

推荐答案

看这个,你需要有点棘手.您需要动态计算textView 的高度,并根据TextView 的高度,返回单元格的高度..

Looking at this,you need to be somewhat tricky. You need to calculate the height of the textView dynamically and based on the Height of the TextView,you need to return the Height for the cell..

这很容易&有点棘手..

It's very easy & somewhat Tricky..

这是计算字符串大小的代码......

This is the code by which you can calculate the size of string....

首先获取String的大小

First get the size of String

NSString *label =  @"Sample String to get the Size for the textView Will definitely work ";
CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
                      constrainedToSize:CGSizeMake(320, 9999)
                          lineBreakMode:UILineBreakModeWordWrap];

over here ....
NSLog(@"%f",stringSize.height);

第二次在单元格中动态创建textView..给出stringSize.height

Secondly dynamically create the textView in the cell..giving the stringSize.height

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    //}

    NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];

    NSString *string = [d valueForKey:@"Description"];
    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];

    UITextView *textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+10)];
    textV.font = [UIFont systemFontOfSize:15.0];
    textV.text=string;
    textV.textColor=[UIColor blackColor];
    textV.editable=NO;
    [cell.contentView addSubview:textV];
    [textV release];

    return cell;
}


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

    NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
    NSString *label =  [d valueForKey:@"Description"];
    CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999) 
                              lineBreakMode:UILineBreakModeWordWrap];

    return stringSize.height+25;

} 

在给我的手指带来如此多的痛苦之后,......我认为这是足够的代码......&一定会帮助解决您的问题..

After giving so much pain to my fingers,......I think this is enough code...& will surely help to solve your problem..

祝你好运

这篇关于如何在 UITextView 的基础上制作一个带有 UITextView 的 UITableViewCell,动态调整其高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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