2011-01-20 36 views
10

Tôi đã có thể tìm thấy hướng dẫn về sửa đổi tiêu đề bảng trên iOS bằng UITableView - nhưng havent có thể tìm thấy bất kỳ thông tin nào cho phát triển mac. Có ai biết về bất kỳ tài nguyên tốt/bước để sửa đổi sự xuất hiện của bảng?Sửa đổi tiêu đề bảng trên máy Mac

Zach

Trả lời

38

Để thay đổi cách các tiêu đề bảng xuất hiện bạn cần phải phân lớp NSTableHeaderCell, thực hiện bản vẽ tùy chỉnh của riêng bạn trong một trong những phương pháp vẽ của nó, sau đó thay thế các tế bào tiêu đề của mỗi cột với một thể hiện của lớp con của bạn.

Bạn cũng có thể thấy rằng bạn cần phải phân lớp NSTableHeaderView để vẽ bất kỳ không gian nào không hiển thị ô tiêu đề và thay thế cornerView của chế độ xem bảng.

Điều này sẽ giúp bạn bắt đầu:

for (NSTableColumn *column in [tableView tableColumns]) { 
    [column setHeaderCell: 
     [[[MyHeaderCell alloc] 
         initTextCell:[[column headerCell] stringValue]] 
         autorelease]]; 
} 

Và đây là một điểm khởi đầu cho một lớp con của NSTableHeaderCell:

@interface MyHeaderCell : NSTableHeaderCell 
{ 
} 
- (void)drawWithFrame:(CGRect)cellFrame 
      highlighted:(BOOL)isHighlighted 
       inView:(NSView *)view; 
@end 

@implementation MyHeaderCell 

- (void)drawWithFrame:(CGRect)cellFrame 
      highlighted:(BOOL)isHighlighted 
       inView:(NSView *)view 
{ 
    CGRect fillRect, borderRect; 
    CGRectDivide(cellFrame, &borderRect, &fillRect, 1.0, CGRectMaxYEdge); 

    NSGradient *gradient = [[NSGradient alloc] 
     initWithStartingColor:[NSColor whiteColor] 
       endingColor:[NSColor colorWithDeviceWhite:0.9 alpha:1.0]]; 
    [gradient drawInRect:fillRect angle:90.0]; 
    [gradient release]; 

    if (isHighlighted) { 
     [[NSColor colorWithDeviceWhite:0.0 alpha:0.1] set]; 
     NSRectFillUsingOperation(fillRect, NSCompositeSourceOver); 
    } 

    [[NSColor colorWithDeviceWhite:0.8 alpha:1.0] set]; 
    NSRectFill(borderRect); 

    [self drawInteriorWithFrame:CGRectInset(fillRect, 0.0, 1.0) inView:view]; 
} 

- (void)drawWithFrame:(CGRect)cellFrame inView:(NSView *)view 
{ 
    [self drawWithFrame:cellFrame highlighted:NO inView:view]; 
} 

- (void)highlight:(BOOL)isHighlighted 
     withFrame:(NSRect)cellFrame 
      inView:(NSView *)view 
{ 
    [self drawWithFrame:cellFrame highlighted:isHighlighted inView:view]; 
} 

@end 
+1

Đó là một câu trả lời tuyệt vời; Tôi ước tôi đã tìm thấy nó hai ngày trước. Điều đó sẽ có nghĩa là ít hơn 48 giờ đập đầu tôi trên bàn làm việc. :-) – nomothetis

+0

Cảm ơn, bạn vừa mới tiết kiệm cho tôi nhiều giờ! –

+0

Tôi có điều này nhưng tôi muốn nó với biên giới tại các ranh giới tableheadercell, làm thế nào để đạt được điều đó? – Jesus

0

Mã để thay đổi tiêu đề cột trong tableView:

NSString *title = @"Your Title"; 
NSTableColumn *yourColumn = self.tableView.tableColumns.lastObject; 
     [yourColumn.headerCell setStringValue:title]; 
Các vấn đề liên quan