ios - Add subviews to uitableviewcell dynamically -
alright i'm trying seems hard reason (in other words can't seem figure out i'm doing wrong/missing)
first started writing cell in code. way did required result cell not expand correct height.
now i've switched using xib build view. need following:
the blue part need 0 in case there no subviews label , image fill entire cell. when there subviews should added , cell needs expand required height.
here code wrote add subviews container.
override func draw(_ rect: cgrect) { super.draw(rect) drawsubcells() } func drawsubcells() { // check if there subcells if let cells = subcells { let count = cgfloat(cells.count) let contentviewframe = cgrect(x: self.frame.width * 0.2, y: cell_contentview.frame.origin.y, width: self.frame.width * 0.8, height: 40 * count) cell_contentview.frame = contentviewframe var = 0 cell in cells { // create view each cell let cellview = uiview() let cellframe = cgrect(x: 0, y: cgfloat(i * 40), width: contentviewframe.width, height: 40) cellview.frame = cellframe // add image view each cell let imageview = uiimageview(image: cell.cellimage) let imageviewframe = cgrect(x: 0, y: (cellframe.height - cellframe.height / 2) / 2, width: cellframe.width * 0.2, height: cellframe.height / 2) imageview.frame = imageviewframe imageview.contentmode = .scaleaspectfit // add label each cell let celllabel = uilabel() let celllabelframe = cgrect(x: imageviewframe.width, y: 0, width: cellframe.width - imageviewframe.width, height: 40) celllabel.frame = celllabelframe //celllabel.font = cell.cellfont celllabel.textcolor = cell.celltextcolor celllabel.text = cell.celltext.uppercased() celllabel.minimumscalefactor = 15 // add imageview , label subcell view cellview.addsubview(imageview) cellview.addsubview(celllabel) // add subcell cell cell_contentview.addsubview(cellview) += 1 } } } however doesn't work. think has constraints being incorrect in xib.
i uploaded project (which test project) here. i'm hoping able me out this. can't seem figure out i'm going wrong. can find class wrote entire view programmatically in project.

Comments
Post a Comment