テーブルビューにカスタムのテーブルビューセルがあります。
このメソッドでそのセルの高さを設定できます。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{ }
しかし、uitableviewcell ファイル (例: FlightDetailCell.m) で高さを動的に変更することは可能ですか?
テーブルビューにカスタムのテーブルビューセルがあります。
このメソッドでそのセルの高さを設定できます。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{ }
しかし、uitableviewcell ファイル (例: FlightDetailCell.m) で高さを動的に変更することは可能ですか?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
更新された高さを設定するために、上記のメソッドが呼び出されるたびにセルの高さを計算する必要があります。そのとき、更新された動的高さを渡す必要があります。
カスタムセルデリゲートを使用してそれを行うことができます:
私の例を試すには:
ViewController
TableViewCell
それが役に立てば幸い!
例:
テーブルビューを使用したView Controllerの実装:
#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
iOS8 Self-Sizing Cells 機能が導入されました。私はそれについてのチュートリアルを提供し、内部で何が起こるかを説明しました. これは非常にシンプルで、開発時間を短縮します。
http://kemal.co/index.php/2014/07/an-example-of-self-sizing-cells-introduced-in-ios8/
はい、できますが - (void)[reloadData];
、テーブル ビューを更新するには呼び出す必要があります。
reloadDataを参照