gpt4 book ai didi

ios - 调整 UITableViewCell 框架问题的大小

转载 作者:行者123 更新时间:2023-12-01 17:17:45 24 4
gpt4 key购买 nike

我正在尝试调整我的 UITableViewCell's框架通过:

 [cell setFrame:CGRectMake(cell.frame.origin.x, 
cell.frame.origin.y,
cell.frame.size.width,
cell.frame.size.height+25)];

但是,在我这样做之后它没有调整大小......这是为什么?

这很奇怪,好像我添加了 UIToolBar进入单元格,它会调整大小,但是当我添加 UIView 时,它不会:
[cell.contentView addSubview:sideSwipeView];

最佳答案

这是它的长短:

  • 您的单元格宽度由它所在的表格 View 的宽度决定。

  • [编辑:如果它是分组表格 View ,则单元格比表格 View 宽度窄 20-60 像素,具体取决于您使用的是 iPhone 还是 iPad。]
  • 您的单元格高度由 heightForRowAtIndexPath 确定方法。

  • 如果您手动设置单元格的框架,那么它将毫无用处,除非您使用子类单元格,您想根据单元格的尺寸添加 subview 。

    即使在这种情况下,建议使用 rectForRowAtIndexPath:(NSIndexPath*)indexPath 从 tableview 获取单元格的框架。方法,然后将该框架设置为单元格的框架(在将框架的原点 Y 设置为 0 之后)。

    我不太确定 UIToolBar,但是您的 subview 的框架不会在更改单元格框架时改变。

    也许如果您能告诉我们您想要达到的目标,我们可以为您提供解决方案?

    --------------------编辑--------

    因此,您需要在点击时动态地将 subview 添加到单元格并根据新 subview 调整其高度。这会变得毛茸茸的,所以这里是:

    在您的 .h 文件中声明:
    BOOL subviewAdded;

    在您的 .m 文件中,在 init 中,执行以下操作:
    subviewAdded = NO;

    假设您希望单元格的高度在没有 subview 的情况下为 50,在 subview 的情况下为 100。因此,您的 heightForRow 方法应该是:
    - (CGFloat)tableView:(UITableView *)tableView 
    heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (subviewAdded?100.0f:50.0f);
    }

    这意味着最初由于 subviewAdded 为 NO,所有单元格的高度都会变小。

    现在,要在点击时将 subview 添加到单元格,并动态更改其高度,请在您的 didSelectRow 方法中执行此操作:
    - (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Get the cell at this indexPath

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];

    if(subviewAdded)
    {
    subviewAdded = NO;

    for(int i = 0; i < [thisCell.contentView.subviews count]; i++)
    {
    UIView *thisSubview = [thisCell.contentView.subviews objectAtIndex:i];
    [thisSubview removeFromSuperview];
    }
    }
    else
    {
    UIView *someView = [[UIView alloc] initWithFrame:someFrame];

    [thisCell.contentView addSubview:someView];
    [someView release];

    subviewAdded = YES;
    }

    NSMutableArray *array = [NSMutableArray array];

    [array addObject:indexPath];

    [tableView reloadRowsAtIndexPaths:array
    withRowAnimation:UITableViewRowAnimationFade];
    }

    所以这里会发生的是你正在向你点击的这个单元格添加一个 subview 。重新加载此单元格将调用 heightForRowAtIndexPath并做一个漂亮的淡入淡出动画并更改您的桌面 View 高度。

    重要提示:理想情况下,您应该维护一个带有 bool 值的 NSNumbers 数组。数组大小应与您拥有的 tableview 单元格的数量相同。

    在 heightForRow 中,您将检查这个数组,而不是对整个 tableView 使用单个 bool 值。这将确保您可以为不同的单元格设置不同的高度。

    这看起来像:
    - (CGFloat)tableView:(UITableView *)tableView 
    heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL thisBool = (BOOL)[[booleanArray objectAtIndex:indexPath.row] boolValue];

    return (thisBool?100.0f:50.0f);
    }

    我没有在此处发布所有代码,因为它是隐含的,并且我发布的内容应该使您能够很好地完成 bool 数组的事情。

    不管怎样,你来了。我刚刚自己测试了这段代码,所以它可以工作:)

    关于ios - 调整 UITableViewCell 框架问题的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996775/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com