2012-05-30 77 views
0

我在tableViewController中有以下代碼。 cellForRowAtIndexPath沒有被調用。我不知道爲什麼。你能幫我嗎?cellForRowAtIndexPath不在RSS提要閱讀器應用程序中調用

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

是numberOfRowsInSection:被調用?當時的[self.feeds count]> 0? – gaige

+0

@giage你是對的,[self.feeds count]是0.在foundCharacters方法中,[self.feeds addObject:string];不添加「字符串」self.feeds NSMutableArray。儘管如此,「字符串」不爲空。 –

回答

1

你最有可能只需要告訴的tableView重新加載數據,一旦解析器完成:

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

但是還有更多的故事有點太。您的解析代碼可能並不總是獲得您的title XML元素的全部內容。你應該創建一個名爲東西類的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]; 
+0

非常感謝Josh修復代碼:) –