【问题标题】:Modifying Table Headers on Mac在 Mac 上修改表头
【发布时间】:2011-06-12 19:15:46
【问题描述】:

我已经能够找到有关使用 UITableView 在 iOS 上修改表头的教程 - 但无法找到任何有关 mac 开发的信息。有谁知道修改表格外观的任何好的资源/步骤?

扎克

【问题讨论】:

    标签: objective-c cocoa macos interface-builder


    【解决方案1】:

    要更改表格标题的显示方式,您需要继承 NSTableHeaderCell,在其中一种绘图方法中执行您自己的自定义绘图,然后用您的子类的实例替换每列的标题单元格。

    您可能还会发现您需要继承 NSTableHeaderView 以绘制任何不可见标题单元格的空间,并替换表格视图的cornerView

    这应该让你开始:

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

    这里是 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
    

    【讨论】:

    • 这是一个很好的答案;我希望我能在两天前找到它。那将意味着我的头撞在桌子上的时间减少了 48 小时。 :-)
    • 谢谢,你帮我节省了几个小时的谷歌搜索时间!
    • 我有这个,但我希望它在 tableheadercell 边界有边框,如何实现?
    • 非常感谢 :) 如果我只想设置单色(在高亮时更改),您能告诉我吗?
    【解决方案2】:

    tableView 中更改列标题的代码:

    NSString *title = @"Your Title";
    NSTableColumn *yourColumn = self.tableView.tableColumns.lastObject;
            [yourColumn.headerCell setStringValue:title];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-23
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2015-07-06
      • 2014-12-11
      相关资源
      最近更新 更多