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 回答