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

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

问题描述

我想有一个类似于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!

请帮助我,我绝望了

Tony

推荐答案

看着这个,你需要有点棘手。你需要动态地计算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天全站免登陆