Objective c RSS源阅读器应用程序中未调用CellForRowatineXpath
我在tableViewController中有以下代码。未调用cellForRowAtIndexPath。我不知道为什么。你能帮忙吗Objective c RSS源阅读器应用程序中未调用CellForRowatineXpath,objective-c,uitableview,Objective C,Uitableview,我在tableViewController中有以下代码。未调用cellForRowAtIndexPath。我不知道为什么。你能帮忙吗 - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO;
- (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;
}
您很可能只需要告诉tableView在解析器完成后重新加载数据:
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[tableView reloadData];
}
不过,故事还有一点内容。解析代码可能并不总是能够获得title
XML元素的完整内容。您应该创建一个名为currentTitle
的类NSMutableString属性,然后修改代码:
-(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];
正在呼叫第节中的NumberOfRowsin吗?当时[self.feeds count]>0吗?@giage你说得对,[self.feeds count]是0。在foundCharacters方法中,[self.feeds addObject:string];未将“字符串”添加到self.feeds NSMutableArray。虽然“字符串”不是空的。