ios - How to add subviews to a tableview cell dynamically -
i created custom tableview cell in ib. add scroll view subview of cell's contentview, , create iboutlet in tableview cell subclass, , make connection. problem is, want add subviews cell dynamically, when did in code, nothing happens. cell rendered successfully, scrollview has nothing show (no subviews on it).
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // deque cell // create label depending on model object // (this done code, not in ib. because label content-based) // don't know how many labels in advance [cell.scrollview addsubview: label]; // not working !! ... return cell; } but if add subviews in ib (which means subviews pre-defined), works.
is there ways add subviews cell dynamically ? or maybe put code in wrong place ?
try this
uilabel *label1 = [[uilabel alloc]initwithframe:cgrectmake(8, 8, 130, 30)]; label1.text = @"any text"; label1.textcolor = [uicolor redcolor]; label1.backgroundcolor = [uicolor greencolor]; [cell addsubview:label1];// first try adding cell if need add subview of cell.scrollview
nslog(@"scrollview %@",cell.scrollview);//it should not nil check scrollview content size , frame
for dynamic string you
nsstring *string = @"this text"; cgsize stringsize = [string sizewithfont:[uifont systemfontofsize:[uifont systemfontsize]]]; uilabel *label1 = [[uilabel alloc]initwithframe:cgrectmake(8, 8, stringsize.width+30/*you have adjust 30 u required*/, 30)]; label1.text = string; label1.textcolor = [uicolor redcolor]; label1.backgroundcolor = [uicolor greencolor]; cell.scrollview.contentsize = cgsizemake(label1.frame.size.width+30/*you have adjust 30 u required*/, label1.frame.size.height); [cell.scrollview addsubview:label1];
Comments
Post a Comment