将 NSMutableArray 从 Delegate 文件传递​​到 UIViewController 中的 UITableView

发布于 2024-12-04 02:08:26 字数 2148 浏览 0 评论 0原文

我执行以下操作:

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 技术交流群。

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

发布评论

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

评论(3

贪恋 2024-12-11 02:08:26

以下是您问题的解决方案...

    cell.textLabel.text = [[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"name"];

nameCatalog 是您的 NSMutableArray。 [nameCatalog objectAtIndex:indexPath.row] 返回一个 NSMutableDictionary。您必须使用 valueForKey 从中获取值。

Following is the solution for you problem...

    cell.textLabel.text = [[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"name"];

nameCatalog is your NSMutableArray. [nameCatalog objectAtIndex:indexPath.row] returns a NSMutableDictionary. And you have to get the value by using valueForKey from that.

朦胧时间 2024-12-11 02:08:26
int index = indexPath.row;
NSString* name = [[nameCatalog objectForKey:@"name"]objectAtIndex:index];
NSString* URL = [[nameCatalog objectForKey:@"url"]objectAtIndex:index];

如果 nameCatalog 是字典数组,那么

int index = indexPath.row;
NSString* name = [[nameCatalog objectAtIndex:index]objectForKey:@"name"];
NSString* URL = [[nameCatalog objectAtIndex:index]objectForKey:@"url"];
int index = indexPath.row;
NSString* name = [[nameCatalog objectForKey:@"name"]objectAtIndex:index];
NSString* URL = [[nameCatalog objectForKey:@"url"]objectAtIndex:index];

if nameCatalog is an array of dictionary, then

int index = indexPath.row;
NSString* name = [[nameCatalog objectAtIndex:index]objectForKey:@"name"];
NSString* URL = [[nameCatalog objectAtIndex:index]objectForKey:@"url"];
那伤。 2024-12-11 02:08:26

用这个

cell.textLabel.text=[[catalogue objectAtIndex:indexPath.row] valueForKey:@"name"];

use this one

cell.textLabel.text=[[catalogue objectAtIndex:indexPath.row] valueForKey:@"name"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文