初始化 NSMutableArray

发布于 2024-11-17 21:24:25 字数 1990 浏览 7 评论 0原文

我对 xcode 3 非常陌生,我确实需要帮助,

我使用 UITableView 和 XML 开发了我的应用程序来显示内容。

我有 3 个 .xib 文件,分别是 rootViewController 、 SecondViewController 和 mainview 。

所以问题是: 当我尝试在 rootViewController 中执行 didSelectrow 并访问 SecondViewController 中的 NSMutableArray *array 并在推送动画之前将 *array 值替换为 rootViewController 中的新数组值。

我的 SecondViewController 上的数组值第一次更改,但是当我按下后退按钮并选择另一行时,我的 SecondViewController 数组继续读取前一个数组,而不更改为新数组。我尝试初始化但没有运气

这是我在 rootViewController UITableview (didSelectRow) 上的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(2ndController == nil)

2ndController = [[DetailViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];

    //Declare xml NSMutable array record
    ListRecord *record = [self.entries objectAtIndex:indexPath.row];

    //access SecondViewController NSMutable *record
    2ndController.record = [[[NSMutableArray alloc] init] autorelease];   

        //inserting the value from firstview to secondview before push
    2ndController.record = record;



    //push animation
    [self.navigationController pushViewController:2ndController animated:YES];



}

这是我的第二个视图控制器:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    switch(indexPath.section)
    {
        case 0:

            [cell setText:record.name];
            break;
        case 1:

            [cell setText:record.Age];
            break;
        case 2:
        [cell setText:record.summary];
            break;
    }

    return cell;
}

希望有人可以帮助我..

提前感谢.....

Im very new in xcode 3 and i really need help for this

I developed my apps using UITableView and XML to showed the content.

i had 3 .xib files which it rootViewController , SecondViewController and mainview.

So the problem is:
When i try to executed didSelectrow in rootViewController and access the NSMutableArray *array in SecondViewController and replace the *array value with new array value in rootViewController before pushed animation.

The array value on my SecondViewController was changed for the first time but when i pushed the back button and select the other row, my SecondViewController array kept read the previous array not change to a new one. I try to initialize but no luck

This is my code on rootViewController UITableview (didSelectRow):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(2ndController == nil)

2ndController = [[DetailViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];

    //Declare xml NSMutable array record
    ListRecord *record = [self.entries objectAtIndex:indexPath.row];

    //access SecondViewController NSMutable *record
    2ndController.record = [[[NSMutableArray alloc] init] autorelease];   

        //inserting the value from firstview to secondview before push
    2ndController.record = record;



    //push animation
    [self.navigationController pushViewController:2ndController animated:YES];



}

This is my second view controller :

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    switch(indexPath.section)
    {
        case 0:

            [cell setText:record.name];
            break;
        case 1:

            [cell setText:record.Age];
            break;
        case 2:
        [cell setText:record.summary];
            break;
    }

    return cell;
}

Hope someone can help me..

Thanks in advance.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-11-24 21:24:25

几件事

您做了

2ndController.record = [[[NSMutableArray alloc] init] autorelease];

,然后跟进

[cell setText:record.name];

显然, record 属性似乎不是 NSMutableArray 的实例,所以我认为数组初始化部分不正确,因为你确实这样做了,并且已经提到过,

2ndController.record = record;

但我认为问题是你保留了 UITableViewController 子类。您是否尝试过重新加载数据?

[self.tableView reloadData];

将其添加到 DetailViewControllerviewWillAppear 方法中。

Few things,

You do,

2ndController.record = [[[NSMutableArray alloc] init] autorelease];

and follow it up with

[cell setText:record.name];

Clearly, the record property doesn't seem to be an instance of NSMutableArray so I think the array initialization part is incorrect as you do, and already mentioned,

2ndController.record = record;

But the problem I think is that you are retaining your UITableViewController subclass. Have you tried reloading the data?

[self.tableView reloadData];

Add it in the viewWillAppear method of your DetailViewController.

愁以何悠 2024-11-24 21:24:25

您应该再次查看这两行:

2ndController.record = [[[NSMutableArray alloc] init] autorelease];   

//inserting the value from firstview to secondview before push
2ndController.record = record;

第一行对您没有任何用处。它创建并初始化一个新的 NSMutableArray 并将记录属性设置为该新数组。

但是,然后在下一行中,您再次将相同的“记录”属性设置为不同的对象,以便不再引用第一行中的数组。所以你可能还没有创建过它。

确切地说,这不是你的问题,但这个评论对于评论来说太大了。 :)

You should look at these two lines again:

2ndController.record = [[[NSMutableArray alloc] init] autorelease];   

//inserting the value from firstview to secondview before push
2ndController.record = record;

The first line doesn't do anything useful for you. It creates and initializes a new NSMutableArray and sets the record property to that new array.

But then in the very next line you set the same 'record' property again to a different object and so that array in the first line is no longer referenced. So you might as well not have ever created it.

That's not your issue exactly, but this comment was too big for a comment. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文