繁体   English   中英

从UITableViewController将NSMutableArray传递给DetailView-iPhone

[英]Passing an NSMutableArray to a DetailView from UITableViewController - iPhone

我有一个UITableViewController,当点击一个单元格时会显示另一个UITableViewController。 我有一个NSMutableArray,我想在实例化时将其传递到新的UITableViewController中。

我通常会做这样的事情:

- (void)loadStationList {

 StationListView * listView = [[StationListView alloc] initWithNibName:@"StationListView" bundle:nil];
 listView.dataList = newParser.stationData;
 // ...
 // Pass the selected object to the new view controller.
 NSLog(@"New Parser is %d", [newParser.stationData count]); //This is fine - all objects in array here.
 [self.navigationController pushViewController:listView animated:NO];

}

奇怪的是dataList(新类中的NSMutableArray指针)为空(我正在检查委托方法的行数)。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Data List is %d", [dataList count]);
return [dataList count];

}

我尝试了几种不同的方法(例如,在父类中实例化新的NSMutable数组),但是似乎没有任何效果。 这可能与ARC有关,因为我还不太熟悉ARC。 有人可以帮忙吗?

您如何声明dataListnewParser.stationData 应该是这样

@property (nonatomic, strong) NSMutableArray       *dataList;

无论如何,为了确保您不会丢失任何值,您可能希望将每个元素从newParser.stationData复制/分配给dataList 像这儿:

    NSMutableArray * dataList = [[NSMutableArray alloc] init];
    for (id arrayElement in newParser.stationData) {
        [dataList addObject:arrayElement];
            }

试试这个代替简单的分配listView.dataList = newParser.stationData; ps不用担心效率,这是如此之快,没关系。

我通过在应用程序委托中创建一个实例变量,然后从那里保存并读取数组来解决这个问题。 这看起来确实像是一个弧形错误-它可与其他变量类型一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM