iphone - Dynamically change UITextView height instead of scroll -


i'm trying create simple diary app. in have showposttableviewcontroller want display each post. in tableview have 1 section 2 cells, 1 headline , 1 post's body. headline inside uitextfield , body inside uitextview. declare both this:

uitextfield * postheadline; uitextview * posttext; 

and synthesize them in implementation file. setup tableview's cells in willdisplaycell method. looks this:

- (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell    forrowatindexpath:(nsindexpath *)indexpath {      tableview.allowsselection = no;      cgrect wholewindow = [self.view bounds];     float headlineheight = 40;      cgrect headlinerect = cgrectmake(10, 10, wholewindow.size.width, headlineheight);      cgrect bodyrect = cgrectmake(wholewindow.origin.y, wholewindow.origin.x,      wholewindow.size.width, wholewindow.size.height);      postheadline = [[uitextfield alloc] initwithframe:headlinerect];     posttext = [[uitextview alloc] initwithframe:bodyrect];      nsstring * headline = [[nsstring alloc] initwithformat:@"%@",[currentpostarray      objectatindex:0]];      nsstring * body = [[nsstring alloc] initwithformat:@"%@",[currentpostarray      objectatindex:1]];      postheadline.text = headline;      postheadline.font = [uifont fontwithname:@"helvetica-bold" size:24.0];      posttext.text = body;     posttext.backgroundcolor = [uicolor colorwithred:254.0/255.0 green:255.0/255.0            blue:237.0/255.0 alpha:1];     posttext.font = [uifont fontwithname:@"georgia" size:17.0];     posttext.scrollenabled = no;     posttext.delegate = self;      if (indexpath.row == 0) {         [cell.contentview addsubview:postheadline];     }     if (indexpath.row == 1) {         [cell.contentview addsubview:posttext];         cgrect frame = posttext.frame;         frame.size.height = posttext.contentsize.height;         posttext.frame = frame;     } } 

yep, i'm beginner, leaking memory crazy. question related that. seems uitextfield postheadline gets set twice, can't erase because it's 2 layers of same text. how can solve that?

back original question. cellforrowatindexpath left petty intact. have sat delegate methods uitextview (think it's here problem lies). found solution worked here: uitextview change height instead of scroll.

they this:

- (void)textviewdidbeginediting:(uitextview *)textview {     textview.frame =cgrectmake(textview.frame.origin.x,textview.frame.origin.y,textview. frame.size.width,textview.frame.size.height + 100);  } 

this method should resize textview:

- (void)textviewdidchange:(uitextview *)textview {      cgfloat fontheight = (textview.font.ascender - textview.font.descender) + 1;      cgrect newtextframe = textview.frame;     newtextframe.size = textview.contentsize;     newtextframe.size.height = newtextframe.size.height + fontheight;     textview.frame = newtextframe;  } 

i set row height this:

- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath   *)indexpath {      nsstring * body = [[nsstring alloc] initwithformat:@"%@",[currentpostarray  objectatindex:1]];      cgsize bodysize = [body sizewithfont:[uifont fontwithname:@"georgia" size:17.0]  constrainedtosize:cgsizemake(self.view.frame.size.width,cgfloat_max)];      if (indexpath.section == 0 && indexpath.row == 1) {         return bodysize.height + 100;     }         return 50; } 

the thing works. when user hits return, textview seems expand, works 14 times, cursor hides behind keyboard.

any ideas , tips how solve great!

thanks // anders

edit

i solved "headline gets set twice" problem moving of willdisplaycell method code viewdidload method. , think have localized rezising problem is. think in heightforrowatindexpath method. looks this:

- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath     *)indexpath {      nsstring * body = [[nsstring alloc] initwithformat:@"%@",[currentpostarray    objectatindex:1]];      cgsize bodysize = [body sizewithfont:[uifont fontwithname:@"georgia" size:17.0]   constrainedtosize:cgsizemake(self.view.frame.size.width,cgfloat_max)];      if (indexpath.section == 0 && indexpath.row == 1) {         return bodysize.height + 100;     }         return 50; } 

since array , it's content gets set in viewdidload method, size stays same. therefore think need dynamically increase cell's size after textview's content too. because when write this:

if (indexpath.section == 0 && indexpath.row == 1) {     return 1000; }    

the cell's size increases , user can hit "return" more times before keyboard gets in the way. there way increase cells's height dynamically?

think solved it. these methods did magic trick:

[self.tableview beginupdates]; [self.tableview endupdates]; 

i updated heightforrowatindexpath looks this:

if (indexpath.section == 0 && indexpath.row == 1) {             return posttext.frame.size.height + headlineheight;  }     return 44; 

and added these methods viewdidchange , in viewwillappear (to reset sizes).

[self.tableview beginupdates]; [self.tableview endupdates]; 

that pretty it.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -