gpt4 book ai didi

iphone - 如何制作一个带有 UITextView 的 UITableViewCell,在 UITextView 的基础上动态调整其高度?

转载 作者:行者123 更新时间:2023-12-03 18:12:27 26 4
gpt4 key购买 nike

我想要一个 tablew View ,其行为类似于 Apple 的 iPhone 联系人应用程序:一个 uitableviewcell,里面有一个 uitextview,这样当我在 uitextview 中写入时,uitextview 会增加其高度,因此 uitableviewcell 也会相应增加动态调整其高度。我搜索了整个网络,只找到了部分解决方案并且缺少示例代码!

请帮帮我,我很绝望

托尼

最佳答案

看到这里,你需要有点技巧。您需要动态计算textView的高度,并根据TextView的高度,您需要返回单元格的高度..

这非常简单,但有点棘手..

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

首先获取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

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

}

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

祝你好运

关于iphone - 如何制作一个带有 UITextView 的 UITableViewCell,在 UITextView 的基础上动态调整其高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4238760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com