#import "tableVIEwsVIEwController.h"@implementation tableVIEwsVIEwController#pragma mark -#pragma mark Synthesizers@synthesize table;@synthesize sitesArray;@synthesize imagesArray;#pragma mark -#pragma mark VIEw lifecycle// Implement vIEwDIDLoad to do additional setup after loading the vIEw,typically from a nib.- (voID)vIEwDIDLoad {// Load up the sitesArray with a dummy array : sitesNSArray *sites = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];self.sitesArray = sites;[sites release];UIImage *active = [UIImage imagenamed:@"a.png"];UIImage *ae = [UIImage imagenamed:@"b.png"];UIImage *audio = [UIImage imagenamed:@"c.png"];UIImage *mobile = [UIImage imagenamed:@"d.png"];UIImage *net = [UIImage imagenamed:@"e.png"];UIImage *photo = [UIImage imagenamed:@"f.png"];UIImage *psd = [UIImage imagenamed:@"g.png"];UIImage *vector = [UIImage imagenamed:@"h.png"];NSArray *images = [[NSArray alloc] initWithObjects: active,ae,audio,mobile,net,photo,psd,vector,nil];self.imagesArray = images;[images release];[super vIEwDIDLoad];}#pragma mark -#pragma mark table VIEw datasource methods// required Methods// Return the number of rows in a section-(NSInteger) tableVIEw:(UItableVIEw *)table numberOfRowsInSection:(NSInteger)section{return [sitesArray count];}// Returns cell to render for each row-(UItableVIEwCell *) tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{static Nsstring *CellIDentifIEr = @"CellIDentifIEr";UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr];if (cell == nil) { cell = [[[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleSubTitle reuseIDentifIEr:CellIDentifIEr] autorelease];}// Configure cellNSUInteger row = [indexPath row];// Sets the text for the cell//cell.textLabel.text = [sitesArray objectAtIndex:row];// Sets the imagevIEw for the cellcell.imageVIEw.image = [imagesArray objectAtIndex:row];// Sets the accessory for the cellcell.accessoryType = UItableVIEwCellAccessorydisclosureIndicator;// Sets the detailtext for the cell (subTitle)//cell.detailTextLabel.text = [Nsstring stringWithFormat:@"This is row: %i",row + 1];return cell;}// Optional// Returns the number of section in a table vIEw-(NSInteger) numberOfSectionsIntableVIEw:(UItableVIEw *)tableVIEw{return 1;}#pragma mark -#pragma mark table VIEw delegate methods// Return the height for each cell-(CGfloat) tableVIEw:(UItableVIEw *)tableVIEw heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 78;}// Sets the Title for header in the tablevIEw-(Nsstring *) tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section{return @"Decors";}// Sets the Title for footer-(Nsstring *) tableVIEw:(UItableVIEw *)tableVIEw TitleForFooterInSection:(NSInteger)section{return @"Decors";}// Sets the indentation for rows-(NSInteger) tableVIEw:(UItableVIEw *)tableVIEw indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {return 0;}// This method is run when the user taps the row in the tablevIEw-(voID) tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath{UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Tapped row!" message:[Nsstring stringWithFormat:@"You tapped: %@",[sitesArray objectAtIndex:indexPath.row]] delegate:nil cancelbuttonTitle:@"Yes,I dID!" otherbuttonTitles:nil];[alert show];[alert release];[tableVIEw deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark -#pragma mark Memory management- (voID)dIDReceiveMemoryWarning {NSLog(@"Memory Warning!");[super dIDReceiveMemoryWarning];}- (voID)vIEwDIDUnload {self.table = nil;self.sitesArray = nil;self.imagesArray = nil;[super vIEwDIDUnload];}- (voID)dealloc {[table release];[sitesArray release];[imagesArray release];[super dealloc];}@end
警报的部分
// This method is run when the user taps the row in the tablevIEw-(voID) tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath{UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Tapped row!" message:[Nsstring stringWithFormat:@"You tapped: %@",I dID!" otherbuttonTitles:nil];[alert show];[alert release];[tableVIEw deselectRowAtIndexPath:indexPath animated:YES];}解决方法 在dIDSelectRowAtIndexPath中,您可以只启动另一个视图控制器并显示.您可以从self.navigationController中呈现它,以便在需要时有一个后退按钮.在这里,我展示它以模态呈现:
- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath { // deselect row [tableVIEw deselectRowAtIndexPath:indexPath animated:YES]; // Declare the vIEw controller UIVIEwController *anotherVC = nil; // Determine the row/section on the tapped cell switch (indexPath.section) { case 0: switch (indexPath.row) { case 0: { // initialize and allocate a specific vIEw controller for section 0 row 0 anotherVC = [[VIEwControllerForRowZeroSectionZero alloc] init]; break; } case 1: { // initialize and allocate a specific vIEw controller for section 0 row 1 anotherVC = [[VIEwControllerForRowOnesectionZero alloc] init]; break; } } break; case 1: { // initialize and allocate a specific vIEw controller for section 1 ALL rows anotherVC = [[VIEwControllerForAllRowsSectionOne alloc] init]; break; } } // Get cell textLabel string to use in new vIEw controller Title Nsstring *cellTitleText = [[[tableVIEw cellForRowAtIndexPath:indexPath] textLabel] text]; // Get object at the tapped cell index from table data source array to display in Title ID tappedobj = [sitesArray objectAtIndex:indexPath.row]; // Set Title indicating what row/section was tapped [anotherVC setTitle:[Nsstring stringWithFormat:@"You tapped section: %d - row: %d - Cell Text: %@ - Sites: %@",indexPath.section,indexPath.row,cellTitleText,tappedobj]]; // present it modally (not necessary,but sometimes looks better then pushing it onto the stack - depending on your App) [anotherVC setModalPresentationStyle:UIModalPresentationFormSheet]; // Have the Transition do a horizontal flip - my personal fav [anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; // The method `presentModalVIEwController:animated:` is depreciated in iOS 6 so use `presentVIEwController:animated:completion:` instead. [self.navigationController presentVIEwController:anotherVC animated:YES completion:NulL]; // We are done with the vIEw controller. It is retained by self.navigationController so we can release it (if not using ARC) [anotherVC release],anotherVC = nil;}总结
以上是内存溢出为你收集整理的iphone – Xcode如何将UITableView Cells链接到新的View Controller全部内容,希望文章能够帮你解决iphone – Xcode如何将UITableView Cells链接到新的View Controller所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)