1

我的表格视图中有自定义表格视图单元。

我可以在这种方法中设置该单元格的高度。

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

但是,是否可以在 uitableviewcell 文件中动态更改高度(例如 FlightDetailCell.m)

4

4 回答 4

1

您可以使用自定义单元委托来做到这一点:

试试我的例子:

  • 使用名为的单视图控制器创建新项目ViewController
  • 将示例中的代码粘贴到 .m 文件中
  • 创建名为的自定义单元格TableViewCell
  • 将代码粘贴到 .h 和 .m 文件中
  • 运行并按下单元格。它的高度会改变=)

希望能帮助到你!

例子:

用表视图实现视图控制器:

#import "ViewController.h"
#import "TableViewCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, TableViewCellDelegate>
{
    UITableView* m_tableView;
    NSMutableArray* m_arrayOfCellHeight;
}

@end

@implementation ViewController

#define kCellCount 3
#define kDefaultCellHeight 44
#define kCellIdentifier @"kCellIdentifier"

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, 320, 300)];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    [self.view addSubview:m_tableView];

    m_arrayOfCellHeight = [NSMutableArray new];

    for (int i = 0; i < kCellCount; i++)
        [m_arrayOfCellHeight addObject:@(kDefaultCellHeight)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [m_arrayOfCellHeight[indexPath.row] floatValue];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell* _cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (!_cell)
    {
        _cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }
    _cell.textLabel.text = @"press me";
    _cell.delegate = self;
    return _cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [(TableViewCell*)[m_tableView cellForRowAtIndexPath:indexPath] changeHeight];
}

- (void)cell:(TableViewCell *)cell didChangeHeight:(CGFloat)height
{
    NSIndexPath *indexPath = [m_tableView indexPathForCell:cell];
    m_arrayOfCellHeight[indexPath.row] = @(height);
    [m_tableView beginUpdates];
    [m_tableView endUpdates];
}

@end

细胞界面:

#import <UIKit/UIKit.h>

@protocol TableViewCellDelegate;
@interface TableViewCell : UITableViewCell

@property (nonatomic, weak) id<TableViewCellDelegate> delegate;
- (void) changeHeight;

@end

@protocol TableViewCellDelegate <NSObject>

- (void) cell:(TableViewCell*)cell didChangeHeight:(CGFloat)height;

@end

单元格的实现:

@implementation TableViewCell

- (void) changeHeight
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:didChangeHeight:)])
    {
        CGFloat _float = rand()%70 + 30;
        [self.delegate cell:self didChangeHeight:_float];
    }
}

@end
于 2014-07-31T04:37:08.577 回答
1

每次重新加载时,都需要计算单元格的高度,- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;将调用上述方法以设置更新的高度。那个时候你需要通过更新的动态高度。

于 2014-07-31T04:06:42.567 回答
1

在 iOS8 中引入了 Self-Sizing Cells 功能。我提供了一个关于它的教程,还解释了引擎盖下发生的事情。它非常简单,会增加您的开发时间。

http://kemal.co/index.php/2014/07/an-example-of-self-sizing-cells-introduced-in-ios8/

于 2014-07-31T23:30:21.877 回答
1

是的,您可以,但您必须调用 - (void)[reloadData];以更新表格视图。

重载数据

于 2014-07-31T03:18:02.280 回答