Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在表格视图中点击单元格时,按下新视图_Objective C_Xcode_Uitableview_Didselectrowatindexpath - Fatal编程技术网

Objective c 在表格视图中点击单元格时,按下新视图

Objective c 在表格视图中点击单元格时,按下新视图,objective-c,xcode,uitableview,didselectrowatindexpath,Objective C,Xcode,Uitableview,Didselectrowatindexpath,嘿,伙计们, 我有一个已填充了21个数据的表视图: - (void)viewDidLoad { self.title = NSLocalizedString(@"Glossary", @"Back"); NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Title", @"Meta Description Tag", @"Meta Keywords", @"Headings", @"Image

嘿,伙计们, 我有一个已填充了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项目的这一部分,谢谢。请不要仅仅为了显示不同的项目而创建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
负责设置自己的数据

编辑 您还会注意到,我删除了对app委托的引用。您不需要它来访问导航控制器。导航控制器堆栈中的每个视图控制器都有对它的引用。在彻底分解代码的同时,我还将通过覆盖
glossaryDetailViewController
的getter来考虑视图控制器的创建,如下所示:

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

    return glossaryDetailViewController;
}

如果按此方法操作,则可以删除
If
语句,只需调用
self.glossarydailViewController
请不要创建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
负责设置自己的数据

编辑 您还会注意到,我删除了对app委托的引用。您不需要它来访问导航控制器。导航控制器堆栈中的每个视图控制器都有对它的引用。在彻底分解代码的同时,我还将通过覆盖
glossaryDetailViewController
的getter来考虑视图控制器的创建,如下所示:

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

    return glossaryDetailViewController;
}

如果你这样做,你可以删除
If
语句,只需调用
self.glossaryDetailViewController

你应该编辑你的旧问题,而不是打开一个具有相同目标的新问题。你应该编辑你的旧问题,而不是打开一个具有相同目标的新问题。嘿,谢谢,您是否有关于如何为每个单元格保存数据实例的示例?感谢您,您将NSObject子类化,以创建一个对象,该对象将保存所有相关数据,这些数据将显示在
glossaryDetailViewController
上。然后创建
glossaryArray
来保存这些对象,而不是像现在这样保存NSstring。此外,您还将添加
glossaryDetailItem
作为类型相同的
glossaryDetailViewController
的属性。哇,谢谢,现在我正在使用这段代码,我遇到了一个小问题,我得到了
glossaryDetailViewController.glossaryDetailItem=[glossaryArray objectAtIndex:row]错误,因为它的意思是:错误:请求非结构或联合中的成员“aGlossaryDetail”。有什么想法吗?感谢你现在所拥有的,并把它变成一个新问题。我需要看更多的代码来理解这里发生了什么。嘿,谢谢,你有关于如何为每个单元格保存数据实例的示例吗?感谢您,您将NSObject子类化,以创建一个对象,该对象将保存所有相关数据,这些数据将显示在
glossaryDetailViewController
上。然后创建
glossaryArray
来保存这些对象,而不是像现在这样保存NSstring。此外,您还将添加
glossaryDetailItem
作为类型相同的
glossaryDetailViewController
的属性。哇,谢谢,现在我正在使用这段代码,我遇到了一个小问题,我得到了
glossaryDetailViewController.glossaryDetailItem=[glossaryArray objectAtIndex:row]错误,因为它的意思是:错误:请求非结构或联合中的成员“aGlossaryDetail”。有什么想法吗?感谢你现在所拥有的,并把它变成一个新问题。我需要看更多的代码来理解这里发生了什么。