ios - Dynamic Shapes in UITableView Cell (Swift) -
i new swift , having fits uiview class. have tableview (below) view object (left) , label (right). table works fine , labels appear expected.

where having trouble want view object next label contain various shapes , colors depending on values in array support table...
var tarray = [["row 1","row 2", "row 3", "row 4", "row 5"], ["circle","circle","square","square","diamond"], ["blue","red","green","red","purple"]] so next "row 1", want have blue circle, etc. have linked view object custom class. need approach dynamically create shapes , fill appropriate colors.
in tableviewcontroller, have following, calling symbol class, , getting black circle (i hard-coded circle now)...
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) cell.celllabel.text = tarray[0][indexpath.row] cell.cellsymbol = symbol.init() return cell } in custom symbol class:
import uikit class symbol: uiview { var incolor: string var inshape: string init (in_color: string, in_shape: string) { self.incolor = in_color self.inshape = in_shape super.init(frame: cgrect(x: 0, y: 0, width: 70, height: 70)) } override func drawrect(rect: cgrect) { let path = uibezierpath(ovalinrect: rect) switch self.incolor { case "green" : uicolor.greencolor().setfill() case "blue" : uicolor.bluecolor().setfill() case "yellow" : uicolor.yellowcolor().setfill() case "cyan" : uicolor.cyancolor().setfill() case "red" : uicolor.redcolor().setfill() case "brown" : uicolor.browncolor().setfill() case "orange" : uicolor.orangecolor().setfill() case "purple" : uicolor.purplecolor().setfill() case "grey" : uicolor.darkgraycolor().setfill() default: uicolor.blackcolor().setfill() } path.fill() } required init?(coder adecoder: nscoder) { self.incolor = "" self.inshape = "" super.init(coder: adecoder) } override init(frame: cgrect) { self.incolor = "" self.inshape = "" super.init(frame: frame) } }
i may going wrong , open other approaches entirely. in order compile, had add required init? , override init(frame: cgrect) entires. had put in initialization of self.incolor , .inshape compile, since i'm not passing in parameters those, have nothing assign.
so black circle every time. hard-coded circle keep simple. switch self.incolor nil every time, going down default case.
any suggestions appreciated!!!!
the "always black" error in code has been addressed https://stackoverflow.com/a/35049741/218152. below suggested improvements.
@ibinspectable
less code, more features
replace entire symbol class this:
@ibdesignable class symbol: uiview { var color = uicolor.blackcolor() @ibinspectable var incolor: string = "black" { didset { switch incolor { case "green" : color = uicolor.greencolor() case "blue" : color = uicolor.bluecolor() case "yellow" : color = uicolor.yellowcolor() case "cyan" : color = uicolor.cyancolor() case "red" : color = uicolor.redcolor() case "brown" : color = uicolor.browncolor() case "orange" : color = uicolor.orangecolor() case "purple" : color = uicolor.purplecolor() case "grey" : color = uicolor.darkgraycolor() default: color = uicolor.blackcolor() } } } override func drawrect(rect: cgrect) { color.setfill() let path = uibezierpath(ovalinrect: rect) path.fill() } } use same way use previous 1 (cell.cellsymbol.incolor = ...). have advantage of being visually editable interface builder. not require special initialization (no init).
this implementation has added advantage accept uicolor directly, in cell.cellsymbol.color = ...
further improvements include using tintcolor instead of creating own instance, case-insensitive colors, enum instead of names.
Comments
Post a Comment