Iphone UITableViewCell内的UITextView
根据这一点,我在Iphone UITableViewCell内的UITextView,iphone,ios,objective-c,uitableview,uitextview,Iphone,Ios,Objective C,Uitableview,Uitextview,根据这一点,我在UITableViewCell中有一个UITextView。加载UITableView后,我希望UITextView和UITableViewCell调整其高度 但是我根据教程做了所有事情,UITextView无法显示所有内容,部分UITextView内容仍然需要滚动显示 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)index
UITableViewCell
中有一个UITextView
。加载UITableView
后,我希望UITextView
和UITableViewCell
调整其高度
但是我根据教程做了所有事情,UITextView
无法显示所有内容,部分UITextView
内容仍然需要滚动显示
这是我的密码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *contentTableIdentify = @"contentTableIdentify";
UITableViewCell *cell = nil;
//UILabel *label = nil;
UITextView *textView = nil;
cell = [self.tableView dequeueReusableCellWithIdentifier:contentTableIdentify];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentTableIdentify];
textView = [[UITextView alloc] initWithFrame:CGRectZero];
[textView setTextColor:[UIColor blackColor]];
[textView setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setTag:1];
[textView setEditable:NO];
[[cell contentView] addSubview:textView];
}
NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(320 - (10 * 2), 99999.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!textView)
textView = (UITextView *)[cell viewWithTag:1];
[textView setText:text];
[textView setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (10 * 2);
}
计算单元格高度时,需要考虑文本视图的填充 如果您执行以下操作:
// Add 16px (8+8) to account fr the UITextView padding
CGSize constraint = CGSizeMake(320 - (10 * 2) -16, 99999.0f);
GSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]
CGFloat height = MAX(size.height, 44.0f);
return height + (10 * 2) + (8*2);
您将找到有关此文本视图的更多信息,其中包含默认填充 使用UILabel而不是UITextview或删除填充
textView .contentInset = UIEdgeInsetsMake(-11,-8,0,0);
您是在故事板中制作此单元格,还是使用自动布局?谢谢,我使用UILabel而不是UITextView来解决此问题。