iOS-UITextView中的表视图单元格未正确调整大小
我已经对一个cell类进行了子类化,并且正在使用故事板中的原型。该单元格有一个嵌入式文本视图,可以显示不同长度的文本。 我已经在HeightForRowatineXpath:中设置了单元格高度,但尝试在CellForRowatineXpath:中调整文本视图本身的大小无法正常工作。 它不会在初始加载时调整大小,但会在我将单元格从视图中滚动出来后再返回。但如果我继续前后滚动,它偶尔会再次缩小 代码:iOS-UITextView中的表视图单元格未正确调整大小,ios,textview,height,tableview,cell,Ios,Textview,Height,Tableview,Cell,我已经对一个cell类进行了子类化,并且正在使用故事板中的原型。该单元格有一个嵌入式文本视图,可以显示不同长度的文本。 我已经在HeightForRowatineXpath:中设置了单元格高度,但尝试在CellForRowatineXpath:中调整文本视图本身的大小无法正常工作。 它不会在初始加载时调整大小,但会在我将单元格从视图中滚动出来后再返回。但如果我继续前后滚动,它偶尔会再次缩小 代码: 事实证明,使用自动布局,我只需将顶部空间和底部空间设置为superview(单元视图),由于单元格
事实证明,使用自动布局,我只需将顶部空间和底部空间设置为superview(单元视图),由于单元格高度在heightForRowAtIndexPath中更改:文本视图将自动调整大小。是否使用自动布局?通过在情节提要中添加文本视图并为其提供正确的约束,这将更容易实现。它会随着单元格自动增长,所以您只需要计算单元格高度。是的,我正在使用autoLayout。不过,使用它还是有点新鲜。适当的限制是什么?该单元基本上与Facebook新闻提要单元相同(顶部为用户图像/名称,中间为文本视图,底部为链接横幅视图),这正好帮助我解决了我的确切问题!非常感谢!
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int defaultSize = 0;
int height = 0;
if(indexPath.section%2==0){
defaultSize = 160;
NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
NSString *string = [currentPost valueForKey:@"detailsText"];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
height = stringSize.height + defaultSize;
NSLog(@"String Size Before: %f",stringSize.height);
}else{
defaultSize = 270;
height = defaultSize;
}
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
if(indexPath.section%2==0){
CellIdentifier = @"NewsFeedText";
}else{
CellIdentifier = @"NewsFeedImage";
}
NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
[cell setBackgroundView:[[NewsFeedCellBackground alloc] init]];
[cell.bottomContainer setClipsToBounds:YES];
[cell.bottomContainer.layer setMasksToBounds:YES];
[cell.imagePreview.imageView setContentMode:UIViewContentModeScaleAspectFit];
[cell.bottomContainer setFrame:CGRectMake(0, cell.contentView.frame.size.height-30, cell.contentView.frame.size.width, 30)];
//Data Object
NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section];
//View
cell.userImage.image = [UIImage imageNamed:[currentPost valueForKey:@"userImage"]];
cell.userName.text = [currentPost valueForKey:@"userName"];
cell.createdDate.text = [currentPost valueForKey:@"createdDate"];
cell.patientName.text = [currentPost valueForKey:@"patientName"];
cell.detailsText.text = [currentPost valueForKey:@"detailsText"];
[cell.imagePreview setImage:[UIImage imageNamed:[currentPost valueForKey:@"imagePreview"]] forState:UIControlStateNormal];
[cell.viewCase addTarget:self action:@selector(displayCase:) forControlEvents:UIControlEventTouchUpInside];
//Image
[cell.imagePreview addTarget:self action:@selector(displayImage:) forControlEvents:UIControlEventTouchUpInside];
//Data
cell.viewCase.tag = [[currentPost valueForKey:@"caseID"] intValue];
//Cell Adjustments
NSString *string = [currentPost valueForKey:@"detailsText"];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"String Size After: %f",stringSize.height);
[cell.detailsText setFrame:CGRectMake(cell.detailsText.frame.origin.x, cell.detailsText.frame.origin.y, cell.detailsText.frame.size.width, stringSize.height+10)];
[cell.detailsText setBackgroundColor:[UIColor redColor]];
return cell;
}