1

大家好,我有一个表格视图,其中包含 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 项目的这一部分,谢谢

4

1 回答 1

5

请不要为了显示不同的项目而创建 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负责设置它自己的数据。

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

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

    return glossaryDetailViewController;
}

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

于 2011-04-06T19:19:48.570 回答