ios - Objective C - Remove only UIButtons from superview -


i new objective-c , have ran issue have had hard time solving..

i using shinobidatagrid , using preparecellfordisplay.

to text display this:

if([cell.coordinate.column.title isequaltostring:@"task"]){             textcell.textfield.textalignment = nstextalignmentleft;             textcell.textfield.text = celldataobj.task;         } 

but particular cell, trying add custom button:

if([cell.coordinate.column.title isequaltostring:@"selected"]){               uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];             button.frame = cgrectmake(0, 10 , 32, 32);                if(celldataobj.selected){                  [button setimage:[uiimage imagenamed:@"checked-box.png"] forstate:uicontrolstatenormal];              }else{                  [button setimage:[uiimage imagenamed:@"unchecked-box.png"] forstate:uicontrolstatenormal];              }              button.tag = cell.coordinate.row.rowindex;             [button addtarget:self action:@selector(checkboxpressed:) forcontrolevents:uicontroleventtouchupinside];              [cell addsubview:button];              [button removefromsuperview];          } 

and first run app, checkbox appears. when reload shinobidatagrid checkbox appears again. tried removing button superview, didn't work...any suggestions ? when reload shinobidatagrid 3rd time, checkbox not appear 3rd time, adds when reload shinobidatagrid 2nd time. here whole method:

- (void)shinobidatagrid:(shinobidatagrid *)grid preparecellfordisplay:(sdatagridcell *)cell {      sdatagridtextcell* textcell = (sdatagridtextcell*)cell;      celldata *celldataobj = [cellholderdisplay objectatindex:cell.coordinate.row.rowindex];      textcell.textfield.textalignment = nstextalignmentcenter;      if([cell.coordinate.column.title isequaltostring:@"selected"]){           uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];         button.frame = cgrectmake(0, 10 , 32, 32);            if(celldataobj.selected){              [button setimage:[uiimage imagenamed:@"checked-box.png"] forstate:uicontrolstatenormal];          }else{              [button setimage:[uiimage imagenamed:@"unchecked-box.png"] forstate:uicontrolstatenormal];          }          button.tag = cell.coordinate.row.rowindex;         [button addtarget:self action:@selector(checkboxpressed:) forcontrolevents:uicontroleventtouchupinside];          [cell addsubview:button];          [button removefromsuperview];      }       if([cell.coordinate.column.title isequaltostring:@"task"]){         textcell.textfield.textalignment = nstextalignmentleft;         textcell.textfield.text = celldataobj.task;     }      if([cell.coordinate.column.title isequaltostring:@"bline. start"]){         textcell.textfield.text = celldataobj.baselinedate;     } } 

any suggestions ?

here checkboxpressed method:

- (void)checkboxpressed:(uibutton *)sender {     celldata *cell = [cellholder objectatindex:sender.tag];      if([cell selected] == yes)     {         [[cell actualdate]setstring:@""];         [[cell finisheddate] setstring:@""];         [cell setselected:no];     }     else     {         if(([[cell actualdate] isequaltostring:@""]) && ([[cell finisheddate] isequaltostring:@""]))         {             [[cell actualdate]setstring:[nsstring stringwithformat:@"%@ 8:00:00 am",[self setspecialdateformat:[nsdate date]]]];             [[cell finisheddate]setstring:[nsstring stringwithformat:@"%@ 4:00:00 pm",[self setspecialdateformat:[nsdate date]]]];         }         //actual date not populated finish date populated         else if(([[cell actualdate]isequaltostring:@""]) && !([[cell finisheddate] isequaltostring:@""]))         {             [[cell actualdate]setstring:[cell finisheddate]];         }         //finish date not populated actual date populated         else if(!([[cell actualdate] isequaltostring:@""]) && ([[cell finisheddate] isequaltostring:@""]))         {             [[cell finisheddate]setstring:[cell actualdate]];         }          [cell setselected:yes];      }     [self updateedittedcells:cell];     [self setdisplayholder];      //refresh grid     [gridreference reload]; } 

(it's not entirely clear effect you're after here, i'm assuming have column filled checkboxes, either checked or un-checked according data model)

the problem encountering shinobi data grid re-uses cells, in order optimise memory usage. therefore adding button cell mean button there cell reused (when grid reloaded or scrolled).

you could remove contents of cell @ beginning of preparecellfordisplay:, , you'd have empty cell can add button:

- (void)shinobidatagrid:(shinobidatagrid *)grid preparecellfordisplay:(sdatagridcell *)cell {   if([cell.coordinate.column.title isequaltostring:@"selected"]){     nsarray *cellsubviews = [cell.subviews copy];     (uiview *subview in subviews) {       [subview removefromsuperview];     }     // safe add button   } } 

however, isn't best approach.

you'd better creating custom cell subclass selected column. cell have button, , therefore make better use of cell reuse mechanism.

to this, create subclass of sdatagridcell, , register grid, in same way would. then, when callback in preparecellfordisplay:, know type custom one.

you can find specific instructions on achieving in shinobidatagrid userguide:

https://www.shinobicontrols.com/docs/shinobicontrols/shinobigrids/2.8.0/standard/normal/docs/markdown_files/datagriduserguide.html#how to: creating custom cells

the example you're looking called "creating custom cells", , last of "how-tos", right @ bottom of page.

this example accompanied complete working sample, provided in samples folder in dmg downloaded. difference sample is using datasource helper. code isn't, simple translate populatecell method of data source helper preparecell method used using.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -