具有可变高度的自定义单元

发布于 2025-01-04 01:20:28 字数 1742 浏览 0 评论 0原文

我在 TableView 中使用自定义单元格。

单元格高度是根据单元格 UILabel 中加载的 NSString 计算的。 使用此函数计算大小

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [self getTableViewRow:tableView index:indexPath];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2) + 60;

}

大小计算正确,但是当单元格加载到 uiTableView 时,行具有正确的高度,但单元格没有。

这是我创建单元格的地方,

//Com Custom Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellApresentacao" owner:self options:nil];

    if ([nib count] > 0 )
        cell = self.tvCell;    
    else
        NSLog(@"Falhou a carregar o xib");
}    

NSInteger row = [indexPath row];

if(self.myTableView1 == tableView)
    labelDescricaoApresentacao.text = [listData1 objectAtIndex:row];
else if (self.myTableView2 == tableView) 
    labelDescricaoApresentacao.text = [listData2 objectAtIndex:row];
else
    labelDescricaoApresentacao.text = [listData3 objectAtIndex:row];  

return cell;
}

我尝试使用此方法更改单元格高度

cell.frame.size.height

,但它仍然没有加载正确的高度。

我必须在 customCell 的 xib 中执行任何操作吗? 我应该如何将单元格高度更新为与行大小相同?

I´m using custom cells in a TableView.

The cell height is calculated based on an NSString that is loaded in a UILabel of the cell.
The size is calculated using this function

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [self getTableViewRow:tableView index:indexPath];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2) + 60;

}

The size is calculated correctly but when the cell is loaded to the uiTableView, the row has the correct height but the cell does not have.

This is where i create my cell

//Com Custom Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellApresentacao" owner:self options:nil];

    if ([nib count] > 0 )
        cell = self.tvCell;    
    else
        NSLog(@"Falhou a carregar o xib");
}    

NSInteger row = [indexPath row];

if(self.myTableView1 == tableView)
    labelDescricaoApresentacao.text = [listData1 objectAtIndex:row];
else if (self.myTableView2 == tableView) 
    labelDescricaoApresentacao.text = [listData2 objectAtIndex:row];
else
    labelDescricaoApresentacao.text = [listData3 objectAtIndex:row];  

return cell;
}

i tried to change the cell height in this method using

cell.frame.size.height

but it still didn´t loaded the correct heigth.

Do i have to do anything in the xib of the customCell?
How should i update the cell heigth to the same size has the row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人迟暮 2025-01-11 01:20:28

你在哪里设置cell.frame.size.height?我建议尝试将其放在这里:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.frame = CGRectMake(0,0,320.0f,30.0f);
}

此方法在单元格显示之前被调用,并且是对单元格进行任何视觉更改的好地方。

Where did you set the cell.frame.size.height? I would recommend trying to put it here:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.frame = CGRectMake(0,0,320.0f,30.0f);
}

This method gets called right before the cell is displayed and is a good place to make any visual changes to the cell.

无人接听 2025-01-11 01:20:28

如果您从 NIB(或故事板)加载原型单元格,它们将具有与 UITableView 中定义的相同高度。

picciano 的解决方案在我的应用程序中不起作用。

我改为使用此委托函数,它有效:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
        CGFloat *theCellHeight=xxx;
        //determine here which section/row it is, and the desired height for it, put it in "theCellHeight"
        return theCellHeight;
}

希望它对您有帮助,

备注:尽管如此,似乎无法获取单元格的内容/引用,因为 cellAtIndexPath 在该函数内调用时会冻结应用程序。因此,您必须以编程方式在数组中构建表,然后创建它,这样会使 NIB 的用处降低。

If you load your prototype cells from a NIB (or storyboard), they will have the same height as defined in the UITableView.

Solution from picciano is not working in my app.

I used this delegate function instead, and it works :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
        CGFloat *theCellHeight=xxx;
        //determine here which section/row it is, and the desired height for it, put it in "theCellHeight"
        return theCellHeight;
}

Hope it helped you,

Remark : Nevertheless, it seems there is no way to get the cell's content/reference because cellAtIndexPath freezes the application when called inside that function. So you have to build your table in an Array programatically, then create it, so it makes the NIB less useful.

如若梦似彩虹 2025-01-11 01:20:28

即使您在 heightForRowAtIndexPath 中正确设置了行的高度,单元格的内容将始终具有固定的高度,因为您是从笔尖加载单元格的。要使单元格内容动态更改高度,您需要在 cellForRowAtIndexPath 中以编程方式创建单元格及其子视图,并使用与 heightForRowAtIndexPath: 中使用的相同高度计算

Even though you are correctly setting the height of your row in heightForRowAtIndexPath, your cell's contents will always have a fixed height because your are loading your cell from a nib. To make the cell content change height dynamically you will need to instead create your cell and its subviews programmatically in cellForRowAtIndexPath, using the same height calculation you are using in heightForRowAtIndexPath:

过气美图社 2025-01-11 01:20:28

我的猜测是您没有正确处理单元格的大小调整。

我不确定单元格的框架何时设置,但我猜测它是在您从 tableView:cellForRowAtIndexPath: 方法返回单元格之后设置的,这意味着该单元格被创建或重用,然后它的框架被设置。

您应该做的是覆盖单元格的 setFrame: 方法,并在调用 super 之后修改它的子视图框架以适应新的大小。

您还可以在 Interface Builder 上的子视图上使用自动调整大小蒙版,以便它们自动适应新框架。

My guess is that you aren't handling the resizing of the cell properly.

I'm not sure when the cell's frame is set, but I'm guessing it's set AFTER you return a cell from your tableView:cellForRowAtIndexPath: method, that means that the cell is created or reused, and THEN it's frame is set.

What you should do is override the cell's setFrame: method and, after calling super, modifying it's subviews frames to fit the new size.

You could also use autoresizing masks on it's subviews on Interface Builder so they adapt automatically to the new frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文