【问题标题】:Adjust height UITableViewCell after cellForRowAtIndexPath在 cellForRowAtIndexPath 之后调整高度 UITableViewCell
【发布时间】:2014-05-22 21:45:06
【问题描述】:

我无法根据自己的限制设置UITableViewCell 的高度。

我在UITableView 的每个单元格中都有一个UILabel,其中高度不固定,取决于此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;

    }
}

由于heightForRowAtIndexPath 方法,我还尝试设置高度,但此方法称为之前 cellForRowAtIndexPath 更新我的标签。

我没有更多的想法来解决这个问题。

提前谢谢你。

【问题讨论】:

  • 没有魔法,你需要在heightForRowAtIndexPath方法中确定你的身高并返回。 cellForRowAtIndexPath中不能设置单元格的高度

标签: objective-c dynamic uitableview height


【解决方案1】:
-(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;
    }

【讨论】:

  • 感谢您的回答。顺便问一下,comment_text 的值如何设置?它是您代码中的变量吗?
  • 好的。但是str11 的等价物是在cellForRowAtIndexPath 方法之后 给出的。那是我的问题。调用heightForRowAtIndexPath 后,我无法更新单元格的大小。
  • 你在两种方法中都传递了相同的字符串............现在我认为你的问题将得到解决
  • 这是一个动态字符串。该值在我的数据库中。我以前无法访问它。它来自与RestKit 的映射,它是我通过relationships 访问的嵌套数据。
  • 你从一个数组中的数据库中获取所有数据.....并在所有 tableview 委托 n 数据源方法中传递该数组.....你可以使用该方法获取数组值 [array objectAtIndex:indexPath.row]
【解决方案2】:

感谢 Rohit 的回答,请找到我的工作代码:

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

【讨论】:

  • 我建议不要使用像234.00f 这样的硬编码值。尤其是现在我们有了 iPhone 6/6 Plus(但即使回答了这个问题,我们也有 iPad 屏幕),这可能会产生意想不到的效果。如果您要对这样的值进行硬编码,最好在 UITableViewCell 子类中执行此操作,您可以在其中为不同的设备使用不同的硬编码值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 2010-11-03
相关资源
最近更新 更多