現在、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 |
| |
+-------------------------------+