将 NSMutableArray 从 Delegate 文件传递到 UIViewController 中的 UITableView
我执行以下操作:
myAppDelegate 文件
我正在解析 xml 文件并将其内容设置为 NSMutableArray * Catalogue
,这样:
NSMutableArray * catalogue;
NSMutableDictionary * item;
NSString * currentElement;
NSMutableString *currentName, *currentUrl;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"catalog"]) {
// save values to an item, then store that item into the array...
[item setObject:currentName forKey:@"name"];
[item setObject:currentUrl forKey:@"url"];
[catalogue addObject:[item copy]];
}
}
myUiViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
nameCatalog = [myAudiAppDelegate sharedAppDelegate].catalogue;
NSLog(@"noul sir format %@", nameCatalog);
}
这我的 nameCatalog
看起来像这样:
{
name = "Audi TT Coup\U00e9";
url = "http://host_server/uploads/modeles_pdf/43_TT_Coupe_TT_Roadster_Catalogue_Tarifs_20110428.pdf";
},
{
name = "Audi TT Roadster";
url = "http://host_server/uploads/modeles_pdf/42_TT_Coupe_TT_Roadster_Catalogue_Tarifs_20110428.pdf";
}
我不知道的是......如何从那里获取名称值并将其放入 tableView 中?
这是我的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.text = [nameCatalog objectAtIndex:indexPath.row];
return cell;
}
但我收到以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary isEqualToString:]:无法识别
我应该如何访问nameCatalog
?谢谢
I do the following:
myAppDelegate file
I'm parsing an xml file and set its content to NSMutableArray * catalogue
, this way:
NSMutableArray * catalogue;
NSMutableDictionary * item;
NSString * currentElement;
NSMutableString *currentName, *currentUrl;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"catalog"]) {
// save values to an item, then store that item into the array...
[item setObject:currentName forKey:@"name"];
[item setObject:currentUrl forKey:@"url"];
[catalogue addObject:[item copy]];
}
}
myUiViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
nameCatalog = [myAudiAppDelegate sharedAppDelegate].catalogue;
NSLog(@"noul sir format %@", nameCatalog);
}
This is what my nameCatalog
looks like:
{
name = "Audi TT Coup\U00e9";
url = "http://host_server/uploads/modeles_pdf/43_TT_Coupe_TT_Roadster_Catalogue_Tarifs_20110428.pdf";
},
{
name = "Audi TT Roadster";
url = "http://host_server/uploads/modeles_pdf/42_TT_Coupe_TT_Roadster_Catalogue_Tarifs_20110428.pdf";
}
What I don't know is...how do I get the name value from there to put it in a tableView?
This is my method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.text = [nameCatalog objectAtIndex:indexPath.row];
return cell;
}
But I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized
How should I acces nameCatalog
?Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是您问题的解决方案...
nameCatalog 是您的 NSMutableArray。 [nameCatalog objectAtIndex:indexPath.row] 返回一个 NSMutableDictionary。您必须使用 valueForKey 从中获取值。
Following is the solution for you problem...
nameCatalog is your NSMutableArray. [nameCatalog objectAtIndex:indexPath.row] returns a NSMutableDictionary. And you have to get the value by using valueForKey from that.
如果 nameCatalog 是字典数组,那么
if nameCatalog is an array of dictionary, then
用这个
use this one