初始化NSMutableArray

时间:2011-06-30 23:52:22

标签: objective-c xcode uitableview nsmutablearray didselectrowatindexpath

我是xcode 3的新手,我真的需要帮助

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

我有3个.xib文件,它们是rootViewController,SecondViewController和mainview。

所以问题是: 当我尝试在rootViewController中执行didSelectrow并访问SecondViewController中的NSMutableArray *数组并在推送动画之前用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;
}

希望有人可以帮助我..

提前致谢.....

2 个答案:

答案 0 :(得分:4)

很少,

你做,

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

并用

跟进
[cell setText:record.name];

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

2ndController.record = record;

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

[self.tableView reloadData];

使用viewWillAppear的{​​{1}}方法添加它。

答案 1 :(得分:1)

你应该再看一下这两行:

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

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

第一行对您没有任何帮助。它创建并初始化一个新的NSMutableArray,并将record属性设置为该新数组。

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

这不是你的问题,但这个评论太大了,不能发表评论。 :)