大佬教程收集整理的这篇文章主要介绍了iphone – Xcode如何将UITableView Cells链接到新的View Controller,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#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 : sites NSArray *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 cell NSUInteger row = [indexPath row]; // Sets the text for the cell //cell.textLabel.text = [sitesArray objectATindex:row]; // Sets the imageview for the cell cell.imageView.image = [imagesArray objectATindex:row]; // Sets the accessory for the cell cell.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]; }
- (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所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。