0

私はSOでたくさんの答えを試しましたが、実際には何もうまくいきませんでした。私はおそらく何か間違ったことをしているので、誰かが私が間違っていることを指摘する必要があります..

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

    NSLog(@"text : %@", self.cell.comment.text);

    NSString *text = self.cell.comment.text;
    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;
}

「NSConcreteAttributedString initWithString:: nil value」という理由で「NSInvalidArgumentException」を取得します。これは、セルの高さを設定するまでに self.cell.comment.text が何も取得されていないためです。

多くの人がその答えについて、この方法は問題なく機能するとコメントしているので、何か不足していると思いますか?

編集

ここで self.cell.comment.text を設定しています -

- (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 = [object objectForKey:@"comment"];
[self.cell.comment sizeToFit];

return self.cell;}
4

3 に答える 3

4

すでにコメントしている問題は、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;
}

この例は機能します。

于 2015-08-13T09:10:09.617 に答える