iphone - Scroll View in Grid TabelView -
i m new iphone development , reason have query regarding how implement scroll view in table view m using following code
#import <uikit/uikit.h> @class scrollviewviewcontroller; @interface scrollviewappdelegate : nsobject <uiapplicationdelegate> { uiwindow *window; scrollviewviewcontroller *viewcontroller; } @property (nonatomic, retain) iboutlet uiwindow *window; @property (nonatomic, retain) iboutlet scrollviewviewcontroller *viewcontroller; @end
////////////////////////////////////////////
#import "scrollviewappdelegate.h" #import "scrollviewviewcontroller.h" @implementation scrollviewappdelegate @synthesize window; @synthesize viewcontroller; - (void)applicationdidfinishlaunching:(uiapplication *)application { // override point customization after app launch [window addsubview:viewcontroller.view]; [window makekeyandvisible]; } - (void)dealloc { [viewcontroller release]; [window release]; [super dealloc]; } @end
///////////////////////////
#import <uikit/uikit.h> @interface mytablecell : uitableviewcell { nsmutablearray *columns; } - (void)addcolumn:(cgfloat)position; @end
//////////////////////////
#import "mytablecell.h" @implementation mytablecell - (id)initwithframe:(cgrect)frame reuseidentifier:(nsstring *)reuseidentifier { if (self = [super initwithframe:frame reuseidentifier:reuseidentifier]) { // initialization code columns = [nsmutablearray arraywithcapacity:5]; [columns retain]; } return self; } - (void)addcolumn:(cgfloat)position { [columns addobject:[nsnumber numberwithfloat:position]]; } - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } - (void)drawrect:(cgrect)rect { cgcontextref ctx = uigraphicsgetcurrentcontext(); // match color , size of horizontal line cgcontextsetrgbstrokecolor(ctx, 0.5, 0.5, 0.5, 1.0); cgcontextsetlinewidth(ctx, 0.25); (int = 0; < [columns count]; i++) { // position vertical line cgfloat f = [((nsnumber*) [columns objectatindex:i]) floatvalue]; cgcontextmovetopoint(ctx, f, 0); cgcontextaddlinetopoint(ctx, f, self.bounds.size.height); } cgcontextstrokepath(ctx); [super drawrect:rect]; } - (void)dealloc { [super dealloc]; [columns dealloc]; } @end
//////////////////////
#import <uikit/uikit.h> @interface rootviewcontroller : uitableviewcontroller { } @end
/////////////////
#import "rootviewcontroller.h" #import "mytablecell.h" @implementation rootviewcontroller #define label_tag 1 #define value_tag 2 #define first_cell_identifier @"trailitemcell" #define second_cell_identifier @"regularcell" - (void)viewdidload { // add following line if want list editable // self.navigationitem.leftbarbuttonitem = self.editbuttonitem; self.title = @"grids!"; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { tableview.separatorstyle = uitableviewcellseparatorstylesingleline; return 19; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *myidentifier = [nsstring stringwithformat:@"myidentifier %i", indexpath.row]; mytablecell *cell = (mytablecell *)[tableview dequeuereusablecellwithidentifier:myidentifier]; if (cell == nil) { cell = [[[mytablecell alloc] initwithframe:cgrectzero reuseidentifier:myidentifier] autorelease]; uilabel *label = [[[uilabel alloc] initwithframe:cgrectmake(0.0, 0, 30.0, tableview.rowheight)] autorelease]; [cell addcolumn:40]; label.tag = label_tag; label.font = [uifont systemfontofsize:12.0]; label.text =@"s.no";// [nsstring stringwithformat:@"%d", indexpath.row]; label.textalignment = uitextalignmentright; label.textcolor = [uicolor redcolor]; label.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight; [cell.contentview addsubview:label]; label = [[[uilabel alloc] initwithframe:cgrectmake(40.0, 0, 70.0, tableview.rowheight)] autorelease]; [cell addcolumn:120]; label.tag = value_tag; label.font = [uifont systemfontofsize:12.0]; // add silly value label.text =@"product id";// [nsstring stringwithformat:@"%d", indexpath.row * 4]; label.textalignment = uitextalignmentright; label.textcolor = [uicolor bluecolor]; label.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight; [cell.contentview addsubview:label]; label = [[[uilabel alloc] initwithframe:cgrectmake(134.0, 0, 70.0, tableview.rowheight)] autorelease]; [cell addcolumn:220]; label.tag = value_tag; label.font = [uifont systemfontofsize:12.0]; // add silly value label.text =@"product name";// [nsstring stringwithformat:@"%d", indexpath.row * 4]; label.textalignment = uitextalignmentright; label.textcolor = [uicolor greencolor]; label.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight; [cell.contentview addsubview:label]; label = [[[uilabel alloc] initwithframe:cgrectmake(230.0, 0, 70.0, tableview.rowheight)] autorelease]; [cell addcolumn:310]; label.tag = value_tag; label.font = [uifont systemfontofsize:12.0]; // add silly value label.text =@"customer name";// [nsstring stringwithformat:@"%d", indexpath.row * 4]; label.textalignment = uitextalignmentright; label.textcolor = [uicolor greencolor]; label.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight; [cell.contentview addsubview:label]; label = [[[uilabel alloc] initwithframe:cgrectmake(320.0, 0, 70.0, tableview.rowheight)] autorelease]; [cell addcolumn:400]; label.tag = value_tag; label.font = [uifont systemfontofsize:12.0]; // add silly value label.text =@"customer product";// [nsstring stringwithformat:@"%d", indexpath.row * 4]; label.textalignment = uitextalignmentright; label.textcolor = [uicolor greencolor]; label.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleheight; [cell.contentview addsubview:label]; } return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic } - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } - (void)viewwilldisappear:(bool)animated { } - (void)viewdiddisappear:(bool)animated { } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations return (interfaceorientation == uiinterfaceorientationportrait); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // releases view if doesn't have superview // release that's not essential, such cached data } - (void)dealloc { [super dealloc]; } @end
////////////
#import <uikit/uikit.h> @interface scrollviewviewcontroller : uiviewcontroller<uiscrollviewdelegate> { } @end
/////////////
#import "scrollviewviewcontroller.h" #import "rootviewcontroller.h" @implementation scrollviewviewcontroller /* // designated initializer. override perform setup required before view loaded. - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { if (self = [super initwithnibname:nibnameornil bundle:nibbundleornil]) { // custom initialization } return self; } */ // implement loadview create view hierarchy programmatically, without using nib. - (void)loadview { rootviewcontroller *rootviewcontrollerlink = [[rootviewcontroller alloc]initwithnibname:@"rootviewcontroller" bundle:nil]; rootviewcontrollerlink.view.tag = 100; /* uiimageview *imgview = [[[uiimageview alloc] initwithimage: [uiimage imagenamed:@"winkler-gnu-blue.png"]] autorelease]; imgview.tag = 100; */ uiscrollview *scrollview = [[[uiscrollview alloc] initwithframe:cgrectmake(0,0,320,480)] autorelease]; scrollview.delegate = self; scrollview.minimumzoomscale = 0.25; scrollview.maximumzoomscale = 2; scrollview.bounces = no; scrollview.showshorizontalscrollindicator = no; scrollview.showsverticalscrollindicator = no; scrollview.contentsize = rootviewcontrollerlink.view.frame.size; scrollview.contentoffset = cgpointmake((rootviewcontrollerlink.view.frame.size.width-320)/2, (rootviewcontrollerlink.view.frame.size.height-480)/2); [scrollview addsubview:rootviewcontrollerlink.view]; self.view = scrollview; } /*- (uiview *)viewforzoominginscrollview:(uiscrollview *)scrollview { return [self.view viewwithtag:100]; } - (bool)touchesshouldbegin:(nsset *)touches withevent:(uievent *)event incontentview:(uiview *)view { return yes; }// default returns yes - (bool)touchesshouldcancelincontentview:(uiview *)view { return yes; } */ // not called if cancancelcontenttouches no. default returns yes if view isn't uicontrol /* // implement viewdidload additional setup after loading view, typically nib. - (void)viewdidload { [super viewdidload]; } */ /* // override allow orientations other default portrait orientation. - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations return (interfaceorientation == uiinterfaceorientationportrait); } */ - (void)didreceivememorywarning { // releases view if doesn't have superview. [super didreceivememorywarning]; // release cached data, images, etc aren't in use. } - (void)viewdidunload { // release retained subviews of main view. // e.g. self.myoutlet = nil; } - (void)dealloc { [super dealloc]; } @end
in above code if set scroll uiimage works if set scroll view rootviewcontroller doesnt work.
please in advance
i didn't read code, please reformat others can read easily.
what mean uiscrollview in uitableview? inside cells? still don't it. fyi uitableview inherits uiscrollview ...
what functionality want achieve? recommend read of samples given apple. there , extensive examples specially regarding uikit.
Comments
Post a Comment