uiview - Layout subviews not working properly -


i have troubles custom view im designing. table display 12 labels, upper left label , lower left label has width*5 of other views. have added views , adjusted frame in layout subviews, labels not appear in view (already checked new views debugger of xcode

 override func layoutsubviews() {     super.layoutsubviews()      let width = self.frame.size.width     let height = self.frame.size.height      let normalwidth = width/10     let normalheight = height/2       var currentorigin = cgpoint(x: 0, y: 0)      let namesize = cgsize(width: normalwidth * 5 - 3, height: normalheight)      labels[0][0].frame = cgrect(origin: currentorigin, size: namesize)      currentorigin.x += normalwidth      j in labels[0]{         j.frame = cgrect(origin: currentorigin, size: cgsize(width: normalwidth - 3, height: normalheight))         currentorigin.x += normalwidth     }      currentorigin.y = normalheight     currentorigin.x = 0     labels[1][0].frame = cgrect(origin: currentorigin, size: namesize)      j in labels[1]{         j.frame = cgrect(origin: currentorigin, size: cgsize(width: normalwidth - 3, height: normalheight))         currentorigin.x += normalwidth     } } 

and constructor im using. according debugger views in superview not visible

 init(frame: cgrect) {     labels = array(count:2, repeatedvalue:array(count:6, repeatedvalue: uilabel() ))     super.init(frame: frame)      in 0..labels.count{         j in 0..labels[i].count{             labels[i][j] = uilabel()             labels[i][j].font = currentfont             labels[i][j].adjustsfontsizetofitwidth = true             labels[i][j].textalignment = nstextalignment.center             labels[i][j].text = "hola mundo"             addsubview(labels[i][j])         }     }      in 0..labels.count{         if let k = delegate?{             labels[i][0].text = k.name(i+1)         }     }       in 0..labels.count{         j in 1..labels[i].count{             labels[i][j].text = "0"         }     } } 

in case has similar troubles here solution found

 labels = array(count:2, repeatedvalue:array(count:6, repeatedvalue: uilabel() )) 

this line generates 2 arrays of uilabels, items of arrays point same instance of uilabel. also:

  labels[0] === labels[1] //they point same instance 

the other mistake iterating in

 in 0..labels.count{     if let k = delegate?{         labels[i][0].text = k.name(i+1)     } } 

the correct thing iterate 1 labels.count first label had have different size.

the correct form instanciate arrays following:

  in 0..2{         labels.append([uilabel]())         j in 0..6{             labels[i].append(uilabel())             labels[i][j].font = currentfont             labels[i][j].adjustsfontsizetofitwidth = true             labels[i][j].textalignment = nstextalignment.center             labels[i][j].text = "hola mundo"                 addsubview(labels[i][j])          } 

hope avoid bug. hard find.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -