Objective c 调整高度UITableViewCell后的cellForRowAtIndexPath
我无法根据约束设置我的Objective c 调整高度UITableViewCell后的cellForRowAtIndexPath,objective-c,dynamic,uitableview,height,Objective C,Dynamic,Uitableview,Height,我无法根据约束设置我的UITableViewCell的高度 我在我的UITableViewaUILabel上的每个单元格中都有一个高度不固定的UILabel 更新标签内容后,我试图在单元格上设置边框,但什么也没发生。请在下面找到我的代码: //On my controller - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static
UITableViewCell
的高度
我在我的UITableView
aUILabel
上的每个单元格中都有一个高度不固定的UILabel
更新标签内容后,我试图在单元格上设置边框,但什么也没发生。请在下面找到我的代码:
//On my controller
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CommentCell";
CommentCell *commentCell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (commentCell == nil) {
commentCell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Comment *comment;
if (commentsArray.count > indexPath.row) {
comment = [commentsArray objectAtIndex:indexPath.row];
}
[commentCell setComment:comment];
[commentCell setCommonFields];
currentCellheight = commentCell.contentLabel.frame.size.height + 40;
DDLogInfo(@"Current Cell Height: %f", currentCellheight);
return commentCell;
}
//On my custom Cell (CommentCell.m)
-(void)setCommonFields {
if (self.comment != nil) {
self.contentLabel.text = self.comment.content;
self.contentLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(self.contentLabel.frame.size.width, FLT_MAX);
CGRect expectedLabelRect = [self.contentLabel.text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
CGRect newFrame = self.contentLabel.frame;
newFrame.size.height = expectedLabelRect.size.height;
self.contentLabel.frame = newFrame;
}
}
由于使用了heightsforrowatindexpath
方法,我还尝试设置高度,但在更新标签之前,此方法被称为cellForRowAtIndexPath
我没有更多的想法来解决这个问题
提前谢谢。感谢Rohit的回答,请找到我的工作代码:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath.row :%ld",(long)indexPath.row);
CGSize contsize={260.00f,3000.00f};
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0];
NSString *strt11=@"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"fontSize indexPath :%f",fontSize.height);
if (fontSize.height>22)
return fontSize.height+50.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse"];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
UILabel *lblcomment=[[UILabel alloc] initWithFrame:CGRectMake(55,23, 160,20)];
[lblcomment setBackgroundColor:[UIColor clearColor]];
[lblcomment setTag:72];
[lblcomment setTextColor:[UIColor grayColor]];
[lblcomment setNumberOfLines:0];
[lblcomment setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];
[lblcomment setText:@"Its rude day on goin...Its rude day on goin...Its rude day on goin..."];
[cell addSubview:lblcomment];
}
UILabel *lblcomment=(UILabel *)[cell viewWithTag:72];
CGSize contsize={260.00f,3000.00f};
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0];
NSString *strt11=@"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];
float h=20;
NSLog(@"re %f",fontSize.height);
if (fontSize.height>22.0f){
h=fontSize.height+7.0;
NSLog(@"heigtaaaaa %f",h);
}
[lblcomment setFrame:CGRectMake(55,23,260,h)];
NSString *strDecodeComment=@""hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
[lblcomment setText:strDecodeComment];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *content=((Comment *)[commentsArray objectAtIndex:indexPath.row]).content;
CGSize maximumLabelSize = CGSizeMake(234.00f, FLT_MAX);
CGRect expectedLabelRect = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
if (expectedLabelRect.size.height > 41) {
return expectedLabelRect.size.height + 81;
}
else
return 81;
}
没有魔法,您需要在
heightforrowatinexpath
方法中确定您的高度并返回它。您无法在cellforrowatinexpath
中设置单元格高度,谢谢您的回答。顺便问一下,如何设置comment\u text
的值?它是代码中的变量吗?好的。但是str11
的等价物是在cellforrowatinexpath
方法之后给出的。那是我的问题。调用heightforrowatinexpath
后,我无法更新单元格的大小。在两种方法中传递相同的字符串。。。。。。。。现在我想你的问题会解决的,这是一个动态的字符串。该值在我的数据库中。我以前没有访问过它。它来自一个带有RestKit
的映射,它是一个嵌套数据,我通过关系访问它。您从一个数组中的数据库获取所有数据…..并在所有tableview委托n数据源方法中传递该数组。。。。。您可以使用该方法[array objectAtIndex:indexath.row]获取数组值,我建议不要使用像234.00f
这样的硬编码值。特别是现在我们有了iPhone6/6 Plus(但即使回答了这个问题,我们也有iPad屏幕),这可能会产生意想不到的效果。如果要对这样的值进行硬编码,最好在UITableViewCell
子类中进行,在该子类中可以为不同的设备使用不同的硬编码值。