2

現在、iOS アプリケーションを Mac に変換しようとしているため、iOS バージョンの動作の一部を複製したいと考えています。モバイル アプリケーションでは、コンテンツと高さに関して動的なテーブル ビューの行を使用し、表示するデータを設定するときに、セルに含まれるビューのフレームを調整します。

例えば:

高さ 100 のセルを開始し、次に別のセルのカスタム高さプロパティを 60 などにリセットし、セルによって表されるデータ オブジェクトを変更し、その調整された高さに収まるように含まれているビューを調整します。

ただし、Mac では期待どおりに動作しないようです。セルの初期化は正常に機能しているように見えますが、セルが再利用されるとすぐに、すべてが手に負えなくなったようです。

これが私のコードです:

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row {


    // Get an existing cell with the 'View' identifier if it exists
    MyCustomTableCellView *result = [tableView makeViewWithIdentifier:@"View" owner:self];

    // There is no existing cell to reuse so create a new one
    if (result == nil) {

        float height = [[rowHeights objectAtIndex:row] floatValue];
        result = [[MyCustomTableCellView alloc] initWithFrame:NSMakeRect(0, 0, self.frame.size.width, height) height:height];

        // This allows the cell to be reused.
        result.identifier = @"View";
    }

    // result is now guaranteed to be valid, either as a reused cell
    result.height = [[rowHeights objectAtIndex:row] floatValue];
    result.data = [arrayToDisplay objectAtIndex:row];

    // Return the result
    return result;

}

およびMyCustomTableCellViewのデータ プロパティのセッター:

-(void)setData:(Data *)d{

    data = d;

    titleLabel.stringValue = d.titleText;
    someLabel.stringValue = d.someText;

    float h = self.height-kInset;
    someLabel.frame = NSMakeRect(someLabel.frame.origin.x, titleLabel.frame.origin.y-h-10, self.bounds.size.width-130, h);

} 

ご覧のとおり、someLabelのテキスト (私の場合) はさまざまに変化しており、セルの寸法に合わせて調整したいと考えています。これは、セルが再利用された場合にのみ発生することを確認できます。

初期化結果:

+-------------------------------+
|titleLabel                     |
+-------------------------------+
|                               |
|someLabel                      |
|                               |
+-------------------------------+

結果の再利用:

+-------------------------------+
|titleLabel                     |
+-------------------------------+
|                               | <---- UNWANTED OFFSET!
+-------------------------------+
|                               |
|someLabel                      |
|                               |
+-------------------------------+
4

1 に答える 1

1

さて、同じ問題に遭遇したすべての人のために:

このコードで新しいデータを設定するとともに、次のようにします。

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row {


    // Get an existing cell with the 'View' identifier if it exists
    MyCustomTableCellView *result = [tableView makeViewWithIdentifier:@"View" owner:self];
    float height = [[rowHeights objectAtIndex:row] floatValue];
    // There is no existing cell to reuse so create a new one
    if (result == nil) {

        result = [[MyCustomTableCellView alloc] initWithFrame:NSMakeRect(0, 0, self.frame.size.width, height) height:height];

        // This allows the cell to be reused.
        result.identifier = @"View";
    }

    NSRect frame = result.frame;
    frame.size.height = height;
    result.frame = frame;
    // result is now guaranteed to be valid, either as a reused cell
    result.height = height;
    result.data = [arrayToDisplay objectAtIndex:row];

    // Return the result
    return result;

}

また、セルのフレームの高さを変更する必要がありました (何らかの理由で)。したがって、基本的にセルのフレームの高さも動的な高さに設定します。

于 2014-01-08T12:05:03.940 に答える