根据UILabel大小扩展UITableViewCell

时间:2012-11-14 11:35:46

标签: iphone objective-c ios uitableview uilabel

我有UITableViewUILabel's:标题,正文,作者和日期,他看起来像这样:

cell55

我想要完成的是,当用户点击单元格本身时,应该在单元格中添加另一个标签,“Body”标签和单元格应根据此标签大小进行扩展。

这样的事情:

cellblsize

我该怎么做?我搜索了stackoverflow,尝试了一些代码片段,但仍未找到正确的解决方案。

谢谢!

编辑1:14.11.12 14:52

我设法用当前文本更改UILabel的大小:

- (CGRect )resizeLabelByFontSize:(UILabel *)customCellLabel withMaxHeightSize:(CGFloat )maxHeight
{
    CGSize maximumLabelSize = CGSizeMake(239, maxHeight);

    CGSize expectedLabelSize = [customCellLabel.text sizeWithFont:customCellLabel.font constrainedToSize:maximumLabelSize lineBreakMode:customCellLabel.lineBreakMode];

    //adjust the label the the new height.
    CGRect newFrame = customCellLabel.frame;
    newFrame.size.height = expectedLabelSize.height;

    return newFrame;
}

但是如何根据新UILabel的大小更改单元格的大小?

6 个答案:

答案 0 :(得分:1)

通过查看问题中的图像

这是为UILabel创建动态框架的方法,看看这个 通过获取UIlabel的高度和宽度,您可以计算整个高度,并可以设置行高UITableView.

- (void)setLabeltextWithVerticalAlignTop:(NSString *)theText
{
CGSize labelSize;
// here  labelSize is hard-wired but could use constants to populate the size

labelSize = CGSizeMake(210, 129);//this is just for example
//now create the Size from textString SO that We  could assign this size to the Label.

 CGSize theStringSize = [theText sizeWithFont:lblTitle.font  constrainedToSize:labelSize lineBreakMode:lblTitle.lineBreakMode];
 lblTitle.frame = CGRectMake(lblTitle.frame.origin.x, lblTitle.frame.origin.y, theStringSize.width, theStringSize.height);
 lblTitle.text = theText;

}

调用上方法要设置描述标签的高度和宽度,您需要传递要在该描述标签上显示的文本。 当你获得该标签的高度时,现在基于此你可以调整TableView的行的高度。

编辑:上面的代码只需为UILabel创建动态框架

You should take a view of this这就是你要找的...... !!!这里你会找到一个示例代码。

编辑:在您编辑问题时,请参阅此处所需的逻辑,将其转换为可运行的代码。

在代码中使用以下方法调用每一行,并在其中进行一些计算。

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight=0.0;
//here it seems cell have 4 subview added on it.
//so if you could calculate the totla hieht of them.

//so what you really need to do.you just use hieght calculative Method for getting hieght of each of three UILabel
//you need to modify  `setLabeltextWithVerticalAlignTop` method .
rowHeight=   [self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"];// suppose it returns some hieght for FisrtLabel.

//suppoose here you get the 20.0 height here

rowHeight= rowHeight+[self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"];

//假设它为secondUIlabel返回了一些高度。

//suppoose here you get the 40.0 height here

rowHeight=  rowHeight+ [self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"];

 // suppose it returns some hieght for ThirdUIlabel.
// suppoose here you get the 15.0 height here
//here you have totla height you just need to add some gapping floating value for all of three UIlabel.so that the could not overlap like as.

 rowHeight= rowHeight+20.0;

  //now you can return that total height
  return rowHeight;
 }

注意:这只是将其转换为可运行代码所需的逻辑。我相信这可以提供帮助。

我希望它可以帮助你。

答案 1 :(得分:0)

实施以下方法

– (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // cast cell, add label, expand labels etc

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath isEqualTo:[tableView indexPathForSelectedRow]] ? /* expanded height */ : 80 /* normal height */;
}

如果您希望即使在选择了另一行后该行仍保持选中状态,则将自定义BOOL属性添加到您的自定义单元格,例如expanded,并使用它来确定身高。

答案 2 :(得分:0)

您可以使用tableView:didSelectRowAtIndexPath

在该方法中,您可以创建代码以取消隐藏身体标签,调整其他所有内容的相对位置。计算行的新高度,然后调用表视图的reloadRowsAtIndexPath:withRowAnimation:方法。

很抱歉,如果没有太多细节,但希望这会让你走上正轨。

答案 3 :(得分:0)

好的,首先......要扩展你需要这样的东西:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

现在是抓住了:

  • 您应该计算UITableViewCell(扩展和非扩展)的大小
  • 实际滚动时这样做可能会很昂贵并且会给您带来不愉快的体验

我的建议:

  • 在您实际结束构建UITableView之前计算双方,因为您希望拥有动态大小。如果不这样做,所有单元格都会扩展相同的大小,你可以使用lammmert所说的。

答案 4 :(得分:0)

NSIndexPath *selectedRow;

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedRow = indexPath;
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 if(indexPath == selectedRow){
    //return your custom value
}
return 100;

}

我认为它看起来像那样

答案 5 :(得分:0)

所以,为了做到这一点,使用花费的UITableViewCell,我创建了2个不同的自定义单元格,在开始时表格显示第一个单元格,当我点击单元格时,表格显示第二个单元格。这很容易 - 是的!

所以我有UIViewController和UITableView来实现表委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if([self.selectedCellIndexPath isEqual:indexPath])
    {
        return [self expandedCellHeight:indexPath];
    }
    else
    {
        return kRegularCellHeight;
    }
}

-(CGFloat)expandedCellHeight:(NSIndexPath *)indexPath
{
    CGSize maxSize = CGSizeMake(303, 200);
    NSString* bodyText  = [[self.data objectAtIndex:indexPath.row] objectForKey:kForumMessagesBody];
    CGSize fitSize = [bodyText sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = 384 - 69 + fitSize.height;
    NSLog(@"expandedHeight: %f",height);
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Answer cell
    if ([self.selectedCellIndexPath isEqual:indexPath])
    {
        cell = [tableView dequeueReusableCellWithIdentifier:[ForumCell expandedAnswerReuseIdentifier]];
        if (cell == nil)
        {
            cell = [ForumCell expandedAnswerCell];
        }
        self.expandedCell = cell;

    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:[ForumCell reqularAnswerReuseIdentifier]];
        if (cell == nil)
        {
            cell = [ForumCell regularAnswerCell];
        }
    }

    cell.labelMedia.text = [self.data objectAtIndex:indexPath.row];

    return cell;
}

我还有自定义单元格,名为ForumCell.hForumCell.m的类,它有2个不同的XIB文件:ForumRegularAnswerCell.xibForumExpandedAnswerCell.xib,我在ForumCell.h内有以下代码{1}}:

+ (NSString*)reqularAnswerReuseIdentifier
{
    return @"RegularAnswerCellReuseIdentifier";
}

+ (NSString*)expandedAnswerReuseIdentifier
{
    return @"ExpandedAnswerCellReuseIdentifier";
}

+ (ForumCell*)regularAnswerCell
{
    NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"ForumRegularAnswerCell" owner:self options:nil];
    ForumCell* result = [objs objectAtIndex:0];

    return result;
}

+ (ForumCell*)expandedAnswerCell
{
    NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"ForumExpandedAnswerCell" owner:self options:nil];
    ForumCell* result = [objs objectAtIndex:0];

    return result;
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super initWithCoder:decoder];
    if (self)
    {
        _originalCellHeight = self.frame.size.height;
        _originalLblBodyHeight = self.lblBody.frame.size.height;
    }
    return self;
}

如果您愿意,也可以使用2个以上的xib。但这是基础。

享受!

相关问题