0

我在 tableViewController 中有以下代码。未调用 cellForRowAtIndexPath。我不知道为什么。你能帮我吗?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    NSURL *rssURL =[[NSURL alloc] initWithString:@"http://www.myfeedsite.com/feed"];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:rssURL];
    [parser setDelegate:self];

    [parser parse];



}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if(elementName)
    {
        self.currentElement = elementName;
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    self.currentElement=nil;

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (!self.currentElement) return;

    if([self.currentElement isEqualToString:@"title"])
    {
        //NSLog(@"%@",string);

        [self.feeds addObject:string];
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.feeds count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    NSLog(@"%@",[self.feeds objectAtIndex:indexPath.row]);

    cell.textLabel.text = [self.feeds objectAtIndex:indexPath.row];

    return cell;
}
4

1 回答 1

1

一旦解析器完成,您很可能只需要告诉 tableView 重新加载数据:

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [tableView reloadData];
}

然而,这个故事还有更多内容。您的解析代码可能并不总能获得titleXML 元素的全部内容。您应该创建一个名为 NSMutableString 的类属性currentTitle,然后修改您的代码:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    self.currentElement = elementName;
    if([self.currentElement isEqualToString:@"title"])
    {
        self.currentTitle = [NSMutableString string];
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([self.currentElement isEqualToString:@"title"])
    {
        //NSLog(@"%@",string);

        [self.feeds addObject:currentTitle];
    }

        self.currentTitle=nil;
    self.currentElement=nil;
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (!self.currentElement) return;

    if([self.currentElement isEqualToString:@"title"])
    {
        //NSLog(@"%@",string);

        [currentTitle appendString:string];
    }

}

关于您刚刚发布的评论:如果您的数组没有得到您正在添加的对象,您可能还没有初始化它。在您的viewDidLoad方法中,添加:

self.feeds = [NSMutableArray array];
于 2012-05-30T16:15:54.983 回答