ios - How to maintain two custom cells in one tableview with segment selection? -
i new ios. have 1 uiviewcontroller
in added 1 tableview , segmented control. if press segmented value = 0, want first custom cell loading images , title , segment value = 1, want display second custom cell uicollectionview
loading of images , title, how can please me? here of code:
menuviewcontroller.m
-(void)callsegmentselected { value=(int)segment.selectedsegmentindex; if (segment.selectedsegmentindex == 0) { nsstring *urlstring = [nsstring stringwithformat:@"http://api"]; nsstring *jsonstring = @""; nsdata *myjsondata =[jsonstring datausingencoding:nsutf8stringencoding]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@"post"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[nsdata datawithdata:myjsondata]]; [request sethttpbody:body]; nserror *error; nsurlresponse *response; nsdata *urldata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nsstring *str=[[nsstring alloc]initwithdata:urldata encoding:nsutf8stringencoding]; if(str.length > 0) { nsdata* data = [str datausingencoding:nsutf8stringencoding]; nserror *error; nslog(@"%@", [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error]); listbannerarray =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil]; [progresshud hide:yes]; [self.tableviewcontest reloaddata]; } else { nslog(@"error"); } }
here cellforrowatindexpath
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"cell"; menutableviewcell *cell = (menutableviewcell *)[tableviewcontest dequeuereusablecellwithidentifier:cellid]; if (cell == nil) { nsarray * mynib; mynib =[[nsbundle mainbundle]loadnibnamed:@"menutableviewcell" owner:self options:nil]; cell = (menutableviewcell *)[mynib lastobject]; } if(value == 0) { @try { if((nsnull *)[[listbannerarray objectatindex:indexpath.row] objectforkey:@"listbanner"] != [nsnull null]) { if([[[listbannerarray objectatindex:indexpath.row] objectforkey:@"listbanner"] length] > 0) { nsurl *imageurl = [nsurl urlwithstring:[[listbannerarray objectatindex:indexpath.row] objectforkey:@"listbanner"]]; [cell.listbanner sd_setimagewithurl:imageurl placeholderimage:[uiimage imagenamed:@"profilepic_bg"]]; } else { } } } @catch (nsexception *exception) { } if((nsnull *)[[listbannerarray objectatindex:indexpath.row] objectforkey:@"examtitle"] != [nsnull null]) { if([[[listbannerarray objectatindex:indexpath.row] objectforkey:@"examtitle"] length] > 0) { cell.examtitle.text = [[listbannerarray objectatindex:indexpath.row] objectforkey:@"examtitle"]; } else { cell.examtitle.text = @"data error"; } } else { cell.examtitle.text = @"data error"; } }
when click on segmentindex=0 screen this
when click on segmentindex=1 this
you can create enum
differentiate segment action.
step 1 : create enum selection.
typedef enum { option_first = 0, option_second } segment_selection;
step 2 : on menuviewcontroller.m
1) create instance of segment_selection
@property(nonatomic) segment_selection segmentselection;
2) assign value segmentselection
-(void)callsegmentselected { if (segment.selectedsegmentindex == 0) segmentselection = option_first else segmentselection = option_second [self.tableviewcontest reloaddata];//for update content in tableview }
step 3 : write below code on cellforrowatindexpath
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if(segmentselection == option_first) { //load content of first segment action } else { //load content of second segment action } }
hope works you.
Comments
Post a Comment