ios - When to remove subview from UITableViewCell? -


i have uitableview searchbar , searchdisplaycontroller. wanted show button when no results found. user search server's database. have nsmutablearrayto store searchresults. delegate method looks this:

- (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope {     nspredicate *predicate = [nspredicate predicatewithformat:@"titulo contains [cd] %@", searchtext];     nsarray *filtrousuario = [self.objects filteredarrayusingpredicate:predicate];     self.searchresults = [[nsmutablearray alloc] initwitharray:filtrousuario];     if (self.searchresults.count < 1) {         uibutton *btn = [uibutton buttonwithtype:uibuttontyperoundedrect];         btn.frame = cgrectmake(0, 0, 320, 50);         btn.showstouchwhenhighlighted = yes;         [btn settitle:@"procurar no banco de dados" forstate:uicontrolstatenormal];         btn.tag = 1;         [self.searchresults addobject:btn];         self.adicionar = yes;     } } 

basically, when there no results, create , add buttonto results array. then, in cellforrowatindexpath, have following:

if (tableview == self.searchdisplaycontroller.searchresultstableview) {         if (self.adicionar == yes) {             cell.textlabel.text = @"";             [cell.contentview addsubview:self.searchresults[indexpath.row]];             self.adicionar = no;         } 

this shows buttonexactly way want, , when cancelbuttonis pressed, or buttonin question, remove searchresultsin case user searches again. [self.searchresults removeallobjects].

the problem that, since i'm reusing cells, subview still there when user searched again. had few options deal this, create property cell , remove subview when buttonwas pressed. opted include line [[cell.contentview viewwithtag:1] removefromsuperview];at beginning of cellforrowatindexpath, when it's called again, deletes subviewsbefore continue.

everything works now. question if best approach or if there's more simple. since app complex app, i'm concerned memory , performance, besides, learn coolest techniques available.

any thoughts?

best flexible term - best in regard...

what have works, isn't allocation of responsibility. better create custom cell subclass , have provide api custom button can added. cell subclass handle cleanup of button in prepareforreuse.

from memory , performance point of view there little difference. using cell subclass more correct.

for performance, it's better not create , destroy button instances. so, better cell subclass create button keep hidden until it's needed, show it. now, prepareforreuse hide button. use little more memory on average - it's trade off...


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -