すでにコメントしている問題は、heightForRowAtIndexPath
セルにデータが入力される前に関数が呼び出されることです。
表示されるすべてのセルに対して、最初に呼び出します
だからあなたはそれを知っています
a)テキストはまだセルに入力されていません
b)アップルは再利用可能なセルを使用するため、他のテキストが内部にある可能性があるため、UITableViewは(異なるテキストで)セルを取得し、サイズを変更してから入力しようとします。
あなたの場合、他のテキストを取得し、セルのサイズをそのサイズに変更してから、前のテキストとは(おそらく)サイズが異なる他のテキストを入力します。
しかし、セル集団内では、いくつかのビジネス ロジック (おそらく配列?) からテキストを設定し、このメソッドで同じテキストを取得できます。
細胞集団を呼び出す場合
cell.comment.text = [self.someArray getObjectAtIndex:index.row];
メソッドでこれを呼び出しますheightForRowAtIndexPath
。
NSString *text = [self.someArray getObjectAtIndex:index.row];
私はあなたの編集を参照してください:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"text : %@", self.cell.comment.text);
NSString *text = [object objectForKey:@"comment"];;
CGFloat width = self.cell.frame.size.width;
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:15];
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:text
attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
CGFloat height = ceilf(size.height);
return height;
}
添加 :
Telは、次のようなセルが必要だと言います:
-------------------------
| bla bla bla |
------------------------
| second longer text |
| over more line |
------------------------
bla bla
テキストと「2 行目に長いテキスト」をどこかに保存する必要があります。
サイズ 2 の配列があるとします。
NSArray * myTextArray = @[@"bla bla", @"second longer text over more line"];
およびセルを移入するとき
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
static NSString *simpleTableIdentifier = @"cell";
self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
self.cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
self.cell.comment.text = [myTextArray objectAtIndex:indexPath.row];
[self.cell.comment sizeToFit];
return self.cell;
}
ビジネス(配列)側からのテキストをチェックする必要があり、視覚的ではないheightForRowAtIndexPath
前に呼び出されるためです。cellForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"text : %@", self.cell.comment.text); // -> this is null because cell is not populated yet.
NSString *text = [myTextArray objectAtIndex:indexPath.row]; -> this is same text as we will take when populating cell and is not random.
CGFloat width = self.cell.frame.size.width;
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:15];
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:text
attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
CGFloat height = ceilf(size.height);
return height;
}
例 :
#import "ViewController.h"
@interface ViewController ()
@property NSArray * myArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myArray = @[@"Some short text",@"Some longer text that take some more space throw more lines",@"bfusdbfjdsfjs fj yfsdy fgsydu fyudsfy fyudsyu fdsy fuysdyuf ydsug fyu sdgyfgsuyff ius fhs fiusdhi ufdshu uifsd ufsdh hfiuds uifdsh fsduih ufdshu hfsd ifshui"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = self.myArray[indexPath.row];
CGFloat width = 300;
UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:15];
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:text
attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
CGFloat height = ceilf(size.height);
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
cell.textLabel.text = self.myArray[indexPath.row];
cell.textLabel.numberOfLines = 0;
return cell;
}
この例は機能します。