ios - Connecting UICollectionViewCell to a simple View Controller Title -
i know automatically going try , same passing data through view controllers page post, not because in scenario different , when uicolletionview introduced because there seems hardly information on compared uitableview.
so heres question have uicollectionview has 3 cells named b , c picture follows:
when click on uicollectionviewcell "a" send me new blank viewcontroller of implements uicollectionviewcell label (which displays letter a) uiviewcontroller title.
my current code follows, know missing -void prepare segue code, should using right? , aware should have uicollcetionview items selected code struggling setup , life of me have been searching internet , 300 posts on here find out how this, there no record or search query must missing important from, can day long using table view have never used comllcetionview before have assumed based on previous code of table view code similar can't find out how want do.
groupsviewcontroller.h
#import <uikit/uikit.h> @interface groupsviewcontroller : uiviewcontroller<uicollectionviewdatasource, uicollectionviewdelegate> @property (weak, nonatomic) iboutlet uicollectionview *groupscollectionview; - (ibaction)celltoggleaction:(id)sender; @end groupsviewcontroller.m
#import "groupsviewcontroller.h" #import "groupshomeviewcontroller.h" #import "customcell.h" @interface groupsviewcontroller () { nsarray *arrayofimages; nsarray *arrayofdescriptions; } @end @implementation groupsviewcontroller { nsstring *reuseidentifier; } - (void)viewdidload { [super viewdidload]; [[self groupscollectionview]setdatasource:self]; [[self groupscollectionview]setdelegate:self]; reuseidentifier= @"smallicon"; arrayofimages = [[nsarray alloc]initwithobjects:@"a.png",@"b.png",@"c.png",nil]; arrayofdescriptions = [[nsarray alloc]initwithobjects:@"a",@"b",@"c",nil]; } -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview { return 1; } -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return [arrayofdescriptions count]; } -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { customcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:reuseidentifier forindexpath:indexpath]; [[cell iconimage]setimage:[uiimage imagenamed:[arrayofimages objectatindex:indexpath.item]]]; [[cell iconlabel]settext:[arrayofdescriptions objectatindex:indexpath.item]]; return cell; } - (void)didreceivememorywarning { [super didreceivememorywarning]; //dispose of resources can recreated. } // toggle view button - (ibaction)celltoggleaction:(id)sender { if([reuseidentifier isequaltostring:@"smallicon"]){ reuseidentifier=@"listview"; [sender setimage:[uiimage imagenamed:@"largeicon"]]; } else if ([reuseidentifier isequaltostring:@"listview"]){ reuseidentifier=@"largeicon"; [sender setimage:[uiimage imagenamed:@"smallicon"]]; } else if ([reuseidentifier isequaltostring:@"largeicon"]){ reuseidentifier=@"smallicon"; [sender setimage:[uiimage imagenamed:@"listview"]]; } [self.groupscollectionview reloaddata]; } //toggled cell sizes - (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath { cgsize cellsize; if([reuseidentifier isequaltostring:@"smallicon"]) cellsize = cgsizemake(100, 130); else if ([reuseidentifier isequaltostring:@"listview"]) cellsize = cgsizemake(320, 50); else if ([reuseidentifier isequaltostring:@"largeicon"]) cellsize = cgsizemake(320, 350); return cellsize; } @end groupshomeviewcontroller.h
#import <uikit/uikit.h> @interface groupshomeviewcontroller : uiviewcontroller @property (strong, nonatomic) iboutlet uiimageview *logoimage; @property (strong, nonatomic) iboutlet uilabel *grouplabel; @end groupshomeviewcontroller.m
#import "groupshomeviewcontroller.h" @interface groupshomeviewcontroller () @end @implementation groupshomeviewcontroller -(void)viewdidload{ [super viewdidload]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end customcell.h
#import <uikit/uikit.h> @interface customcell : uicollectionviewcell @property (weak, nonatomic) iboutlet uiimageview *iconimage; @property (weak, nonatomic) iboutlet uilabel *iconlabel; @end customcell.m
#import "customcell.h" @implementation customcell @end if more information further understand trying accomplish please don't hesitate comment below , thank in advance answers.
you need create property in view controller want navigate on selection of collection view cell.
so in view controller's .h file add
@property (nonatomic, strong) nsstring* titletext; and create custom setter property. set view controller's title.
in view controller's .m file add
- (void)settitletext:(nsstring *)titletext { _titletext = titlefornextvc; // set title of viewcontroller self.title = _titletext; } now need pass string customcell in groupsviewcontroller next view controller.
for implement collectionview:didselectitematindexpath: method in groupsviewcontroller.m
- (void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath { customcell* cell = [self collectionview:collectionview cellforitematindexpath:indexpath]; _titlefornextvc = cell.iconlabel.text; } here titlefornextvc nsstring property in groupsviewcontroller.m class extension.
@interface groupsviewcontroller () { nsarray *arrayofimages; nsarray *arrayofdescriptions; nsstring* _titlefornextvc; } now, in pass string titletext property of next view controller in prepareforsegue:sender:
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:yoursegueidentifier]) { // replace yournextviewcontroller actual class of uiviewcontroller yournextviewcontroller *vc = (yournextviewcontroller *)segue.destinationviewcontroller; vc.titletext = _titlefornextvc; } } and done.

Comments
Post a Comment