在表视图中点击单元格时按下新视图(Push a new view when a cell is tapped in Table View)

编程入门 行业动态 更新时间:2024-10-17 07:32:59
在表视图中点击单元格时按下新视图(Push a new view when a cell is tapped in Table View)

嘿伙计们,我有一个表格视图,其中包含21个数据:

- (void)viewDidLoad { self.title = NSLocalizedString(@"Glossary", @"Back"); NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Title", @"Meta Description Tag", @"Meta Keywords", @"Headings", @"Images", @"Frames", @"Flash Contents", @"Charset", @"Favicon", @"W3C Compatibility", @"Page Rank", @"Alexa Rank", @"Indexed Pages", @"Latest Date Cached By Google", @"Backlinks", @"Dmoz Listing", @"Server Info", @"IP", @"Location", @"Server Type", @"Registrar Info", nil]; self.glossaryArray = array; [array release]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 4; } // Category - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 0) return @"In-Site SEO"; if (section == 1) return @"Inside Analysis"; if (section == 2) return @"Ranks N Stuff"; if (section == 3) return @"Server Info"; return @"Other"; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (section == 0) return 7; if (section == 1) return 3; if (section == 2) return 6; if (section == 3) return 5; return 0; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... NSUInteger row = [indexPath row]; if ( indexPath.section == 1 ) row += 7; if ( indexPath.section == 2 ) row += 10; if ( indexPath.section == 3 ) row += 16; if ( indexPath.section == 4 ) row += 21; cell.textLabel.text = [glossaryArray objectAtIndex:row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } Now this is the code I used to pussh a new view when a cell is tapped: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (self.glossaryDetailViewController == nil) { GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:@"GlossaryDetailViewController" bundle:nil]; self.glossaryDetailViewController = aGlossaryDetail; [aGlossaryDetail release]; } glossaryDetailViewController.title = [NSString stringWithFormat:@"%@", [glossaryArray objectAtIndex:row]]; NewReferencemoi_caAppDelegate *delegate = (NewReferencemoi_caAppDelegate *)[[UIApplication sharedApplication] delegate]; [delegate.glossaryNavController pushViewController:glossaryDetailViewController animated:YES]; }

这段代码完美无缺,但问题是我的表视图中的每个和所有21个元素都打开了我创建的同一个nib文件。 基本上,我想为我的21个元素中的每一个创建1个UIViewController,其中每个元素都有自己的元素描述,而不仅仅是对我的表视图中的所有元素使用1个UIViewController,并且当每个元素被轻击时,每个元素都打开它们自己的元素。视图。 显然,我不知道如何在该部分编码,所以我希望有人可以帮我解决我的iPhone项目的这一部分,谢谢

Hey guys, I have a Table View that has been populated with 21 data:

- (void)viewDidLoad { self.title = NSLocalizedString(@"Glossary", @"Back"); NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Title", @"Meta Description Tag", @"Meta Keywords", @"Headings", @"Images", @"Frames", @"Flash Contents", @"Charset", @"Favicon", @"W3C Compatibility", @"Page Rank", @"Alexa Rank", @"Indexed Pages", @"Latest Date Cached By Google", @"Backlinks", @"Dmoz Listing", @"Server Info", @"IP", @"Location", @"Server Type", @"Registrar Info", nil]; self.glossaryArray = array; [array release]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 4; } // Category - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 0) return @"In-Site SEO"; if (section == 1) return @"Inside Analysis"; if (section == 2) return @"Ranks N Stuff"; if (section == 3) return @"Server Info"; return @"Other"; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (section == 0) return 7; if (section == 1) return 3; if (section == 2) return 6; if (section == 3) return 5; return 0; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... NSUInteger row = [indexPath row]; if ( indexPath.section == 1 ) row += 7; if ( indexPath.section == 2 ) row += 10; if ( indexPath.section == 3 ) row += 16; if ( indexPath.section == 4 ) row += 21; cell.textLabel.text = [glossaryArray objectAtIndex:row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } Now this is the code I used to pussh a new view when a cell is tapped: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (self.glossaryDetailViewController == nil) { GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:@"GlossaryDetailViewController" bundle:nil]; self.glossaryDetailViewController = aGlossaryDetail; [aGlossaryDetail release]; } glossaryDetailViewController.title = [NSString stringWithFormat:@"%@", [glossaryArray objectAtIndex:row]]; NewReferencemoi_caAppDelegate *delegate = (NewReferencemoi_caAppDelegate *)[[UIApplication sharedApplication] delegate]; [delegate.glossaryNavController pushViewController:glossaryDetailViewController animated:YES]; }

This code works perfectly, but the problem is that each and all 21 elements in my table view is opening the one and same nib file that I created. Basically, I want to create 1 UIViewController for each of my 21 elements, where each have their own description of the element, not just using 1 UIViewController for all elements in my Table View, and when each element has been tapped, each open their own view. Apparently, I don't know how to code in that part, so I hope someone can help me out with this part of my iPhone project, thanks

最满意答案

请不要创建21个不同的视图控制器来显示不同的项目。 而是在GlossaryDetailViewController上设置一个属性,该属性包含数据模型项的实例。

考虑一下......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (self.glossaryDetailViewController == nil) { GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:@"GlossaryDetailViewController" bundle:nil]; self.glossaryDetailViewController = aGlossaryDetail; [aGlossaryDetail release]; } glossaryDetailViewController.glossaryDetailItem = [glossaryArray objectAtIndex:row]; [self.navigationController pushViewController:self.glossaryDetailViewController animated:YES]; }

使用此方法使GlossaryDetailViewController负责设置自己的数据。

编辑您还会注意到我删除了对应用程序委托的引用。 您不需要它来访问导航控制器。 导航控制器堆栈中的每个视图控制器都有对它的引用。 当我彻底撕掉你的代码时,我也会通过覆盖glossaryDetailViewController的getter来创建视图控制器,如下所示:

- (GlossaryDetailViewController *)glossaryDetailViewController { if (!glossaryDetailViewController) { glossaryDetailViewController = [[GlossaryDetailViewController alloc] init]; } return glossaryDetailViewController; }

如果你走这条路,你可以删除if语句,然后调用self.glossaryDetailViewController 。

Please do not create 21 different view controllers just to show different items. Instead set a property on GlossaryDetailViewController that holds an instance of your data model item.

Consider this...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (self.glossaryDetailViewController == nil) { GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:@"GlossaryDetailViewController" bundle:nil]; self.glossaryDetailViewController = aGlossaryDetail; [aGlossaryDetail release]; } glossaryDetailViewController.glossaryDetailItem = [glossaryArray objectAtIndex:row]; [self.navigationController pushViewController:self.glossaryDetailViewController animated:YES]; }

Using this approach makes GlossaryDetailViewController responsible for setting it's own data.

Edit You'll also notice that I removed references to the app delegate. You don't need it to get access to the navigation controller. Each view controller in a navigation controller stack has a reference to it. While I'm thoroughly tearing your code apart, I would also factor out the creation of the view controller by overriding the getter for glossaryDetailViewController, like this:

- (GlossaryDetailViewController *)glossaryDetailViewController { if (!glossaryDetailViewController) { glossaryDetailViewController = [[GlossaryDetailViewController alloc] init]; } return glossaryDetailViewController; }

If you go this route, you can remove the if statement and just call self.glossaryDetailViewController.

更多推荐

本文发布于:2023-08-04 10:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   按下   单元格   Push   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!