osx - Setting Background Color of NSTableCellView in Swift -
after searching through , online, i'm struggling figure out concept thought relatively simple. essentially, have table in os x swift app, several columns, , populating data. trying discern how can set background color of each "row" (ideally alternating colors, i'll start 1 color). masterviewcontroller file so;
import cocoa class masterviewcontroller: nsviewcontroller { var minions = [minion]() func setupsampleminion() { minions = minion.fetchminiondata() } override func viewdidload() { super.viewdidload() // view setup here. } } // mark: - nstableviewdatasource extension masterviewcontroller: nstableviewdatasource { func numberofrowsintableview(atableview: nstableview) -> int { return self.minions.count } func tableview(tableview: nstableview, viewfortablecolumn tablecolumn: nstablecolumn?, row: int) -> nsview? { // 1 var cellview: nstablecellview = tableview.makeviewwithidentifier(tablecolumn!.identifier, owner: self) as! nstablecellview let minion = self.minions[row] // 2 if tablecolumn!.identifier == "mycolumn" { // 3 cellview.imageview!.image = nsimage(named: "minion.name!") cellview.textfield!.stringvalue = minion.name! return cellview } return cellview } } func tableview(tableview: nstableview, rowviewforrow row: int) -> nstablerowview? { let mycustomview = myrowview() return mycustomview } class myrowview: nstablerowview { override func drawrect(dirtyrect: nsrect) { super.drawrect(dirtyrect) self.backgroundcolor = nscolor(red: 0.76, green: 0.82, blue: 0.92, alpha: 1) nsrectfill(dirtyrect) } } // mark: - nstableviewdelegate extension masterviewcontroller: nstableviewdelegate { }
while think have of coding right, not seem set background color if row in way. thoughts or overall guidance appreciated. thank you!
if want rows use standard alternating colors rows, there's simple checkbox in attributes inspector table view in ib enable that.
to use non-standard background color, want set row view's backgroundcolor
, not inside of drawrect()
. if change properties of view affect how draws inside of drawrect()
, mark view needing display, provoke call drawrect()
, etc.
it should work set in delegate's tableview(_:didaddrowview:forrow:)
method. that's documented in description of backgroundcolor
property.
with regard attempt @ overriding drawrect()
: setting row view's backgroundcolor
presumably affect how superclass draws. so, setting after calling through super unlikely help. definitely won't affect subsequent nsrectfill()
call. function relies on fill color set current graphics context, implicit. change calling somecolor.set()
.
buy, anyway, there should no need override drawrect()
given can set backgroundcolor
. if want achieve background drawing beyond what's possible setting color, should override drawbackgroundinrect()
, not drawrect()
, anyway.
finally, implementation of tableview(tableview:rowviewforrow:)
should call table view's makeviewwithidentifier(_:owner:)
method first, before creating new view. , should set identifier on new view create. allows table view maintain reuse queue of views, avoid destroying , recreating views.
Comments
Post a Comment