2

特定の行の展開可能なセルを表示するテーブルビューを実装したいので、次のように expandContent を設定すると展開されるカスタム テーブル セルを作成します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSString *shouldExpand = [self.datasource objectAtIndex:indexPath.row];
    if([shouldExpand isEqualToString:@"expand"]){
        [cell setExpandContent:@"Expand"];
    }
    else{
        [cell setTitle:@"a line"];
    }
    return cell;
}

ただし、tableview に行の高さを伝えるには、次のコードを実装する必要があります。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return [cell cellHeight];
}

問題は、heightForRowAtIndexPath メソッドが tableView:cellForRowAtIndexPath: を 1000 回呼び出すことです。データソースに 1000 個のデータが含まれていると、時間がかかりすぎます。

問題を解決するには?

4

3 に答える 3

2

いいえ、最初にセルのサイズを見つけてから、計算された高さを送信する必要があります。tableView:cellForRowAtIndexPathを呼び出さないでください。代わりに再帰が発生します。例えば

         //say suppose you are placing the string inside tableview cell then u need to   calculate cell for example

    NSString *string = @"hello world happy coding";
    CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
     CGSize cellSize = [self.str sizeWithFont:[UIFont systemFontOfSize:17]
                   constrainedToSize:maxSize
                   lineBreakMode:NSLineBreakByWordWrapping];//this will return correct height for text
    return cellSize.height +10; //finally u return your height


于 2013-07-31T05:08:02.350 に答える
1

問題を解決するには?

実際に 1000 行ある場合は、動的な行の高さを使用しないことを検討する必要があります。行の高さをすばやく決定する方法を思いついたとしても、テーブルは各行の高さについて個別に問い合わせる必要があるからです。(実際、実際に 1000 行ある場合は、設計全体を再検討する必要があります。これは、線形インターフェイスで調べるにはデータが多すぎるためです。)

多数の行に対して動的な行の高さを使用する必要がある場合は、少なくとも、セル全体を作成せずに高さをすばやく決定する方法を見つける必要があります。おそらく、行の高さに影響を与える要因を特定し、高さを計算するための非常に単純な方法を考え出すことができます。それができない場合は、各行の高さを 1 回計算し、その結果を行データと共に保存して、データが変更されるまで再度計算する必要がないようにすることをお勧めします。

于 2013-07-31T07:40:34.107 に答える
0

これは、UITableViewCell の高さを動的に設定するために使用したコードです。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary* dict = [branchesArray objectAtIndex:indexPath.row];
    NSString* address = [NSString stringWithFormat:@"%@,%@\n%@\n%@\n%@",[dict objectForKey:@"locality"],[dict objectForKey:@"city"],[dict objectForKey:@"address"],[dict objectForKey:@"contactNumber"], [dict objectForKey:@"contactEmail"]];

    CGSize constraint = CGSizeMake(220, MAXFLOAT);

   CGSize size = [address sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

   CGFloat height1 = MAX(size.height, 110.0f);
   return height1+20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath にフレームを設定するだけでなく、

于 2013-07-31T06:50:03.783 に答える