ios - Create and custom one UIButton for multiple data -
i have array of content follow:
array = [name1,name2,name3...]
and want display these content on label of button. problem hardcore if array have many item , dont want create many button that. please can me findout way generate 1 common button instance these data. if array have 2 item, view display 2 buttons , on... thank much!
p/s: problem solved @janmenjaya answer, here code, still have little bit stuck y position.
func displayfilelist() { in 0..<fileidlist.count { let yref : cgfloat = 35 let title = string(fileidlist.indexof(i)) let button = uibutton(frame: cgrect(x: 0, y: yref * cgfloat(i), width: 919, height: 30)) button.settitle(title, forstate: uicontrolstate.normal) button.settitlecolor(uicolor.blackcolor(), forstate: uicontrolstate.normal) button.backgroundcolor = uicolor.yellowcolor() button.layer.borderwidth = 1; button.layer.bordercolor = uicolor.blackcolor().cgcolor self.filebuttoncontainview.addsubview(button) } }
you can loop through array , create button programmatically dynamic title based on array contents.
follow sample code
sample code snippet :
arrdata = nsmutablearray(); arrdata.addobject("test0"); arrdata.addobject("test1"); arrdata.addobject("test2"); arrdata.addobject("test3"); arrdata.addobject("test4"); var yref : cgfloat = 100 in 0..<arrdata.count{ let title = arrdata.objectatindex(i); let btn = uibutton(type: .custom); btn.frame = cgrectmake(100, yref, 100, 40); btn.settitle(title as? string, forstate: uicontrolstate.normal) btn.backgroundcolor = uicolor.graycolor(); self.view.addsubview(btn); yref += cgrectgetheight(btn.frame) + 10; }
hope helps
happy coding ...
Comments
Post a Comment