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

时间:2011-09-13 07:58:40

标签: iphone delegates nsmutablearray nsmutabledictionary

我执行以下操作:

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类

- (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;
}

但是我收到以下错误: Ťerminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized

我应该如何访问nameCatalog?谢谢

3 个答案:

答案 0 :(得分:4)

以下是解决问题的方法......

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

nameCatalog 是您的NSMutableArray。 [nameCatalog objectAtIndex:indexPath.row] 返回NSMutableDictionary。而且你必须通过使用valueForKey来获取值。

答案 1 :(得分:3)

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"];

答案 2 :(得分:3)

使用这个

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