2014-01-08 28 views
2

我目前正試圖將我的iOS應用程序轉換到Mac上,因此我想從iOS版本複製一些行爲。在移動應用程序中,我使用關於內容和高度的動態表視圖行,並且在設置必須顯示的數據時,調整單元格包含的視圖的框架。在Mac應用程序中重用表格視圖單元格

例如:

我發起小區具有100的高度,然後,對於另一單元,I重置自定義高度屬性,說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      | 
|        | 
+-------------------------------+ 

回答

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; 

} 

我還必須確保更改單元格的框架高度(無論出於何種原因)。所以我基本上也將單元格的框架高度設置爲我的動態高度。

相關問題