Iphone 根据UILabel大小展开UITableViewCell
我有一个Iphone 根据UILabel大小展开UITableViewCell,iphone,objective-c,ios,uitableview,uilabel,Iphone,Objective C,Ios,Uitableview,Uilabel,我有一个UITableView和4个UILabel的:标题、正文、作者和日期,他看起来像这样: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 我想完成的是,当用户单击单元格本身时,应向单元格添加另一个标签,“主体”标签,单元格应根据此标签大小展开 大概是这样的: - (CGFloat)tableView:(UITableView *)tableVi
UITableView
和4个UILabel的
:标题、正文、作者和日期,他看起来像这样:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
我想完成的是,当用户单击单元格本身时,应向单元格添加另一个标签,“主体”标签,单元格应根据此标签大小展开
大概是这样的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
我该怎么做?我搜索了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的大小更改单元格的大小?实现以下方法
– (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 */;
}
如果您希望在选择另一行后该行仍保持选中状态,请将自定义布尔属性添加到自定义单元格中,例如展开的
,,并使用该属性确定高度。您可以使用tableView:DidSelectRowatineXpath
在该方法中,您可以创建代码来取消隐藏主体标签,调整其他所有内容的相对位置。计算行的新高度,然后调用表视图的reloadRowsAtIndexPath:withRowAnimation:方法
抱歉,如果没有太多的细节,但希望这能让你走上正轨。好的,首先。。。要扩展,您需要以下内容:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
现在有一个问题:
- 您应该计算UITableViewCell的大小(展开和未展开)
- 在你实际滚动的时候这样做可能会很昂贵,而且会给你带来不好的体验
我的忠告是:
- 在您实际完成构建
UITableView
之前,请计算两侧,因为您需要动态大小。如果你不这样做,所有的细胞都会有相同的大小扩大,你可以使用拉默特说
通过看到有问题的图像
下面是为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创建动态框架
这就是你要找的。。。。!!!。在这里,您还可以找到一个示例代码
EDIT:当您编辑问题时,请看,这就是您需要将其转换为可运行代码的逻辑。
在为每一行调用的代码中使用下面的方法,并在其中进行一些计算
- (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返回一些hight
//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;
}
注意:这只是将其转换为可运行代码所需的逻辑。我相信这会有所帮助。
我希望它能对您有所帮助。nsindepath*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;
}
我想它看起来会像这样,所以,为了做到这一点,我使用扩展的UITableViewCell创建了两个不同的自定义单元格,开始时表格显示第一个单元格,当我单击单元格时,表格显示第二个单元格。就这么简单-耶
因此,我使用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.h
和ForumCell.m
的类,它有两个不同的XIB文件:ForumRegularAnswerCell.XIB
和forumExpandandanswercell.XIB
,我在ForumCell.h
中有以下代码:
+ (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。但这是最基本的
享受吧 你试过那个链接了吗,在我的回答中提到。。!!!