2014-12-03 65 views
1

我知道它已经多次说明了这个问题,但似乎我无法找到解决方案与我的情况。我有一个应用程序在视图控制器内有一个容器视图控制器,它是一个表视图控制器。在这个表格视图中加载了一些rss提要。当我打开我的应用程序没有互联网连接时,我的数据没有出现,但是当我将我的应用程序连接到互联网后,我的桌子应该重新加载后,我拉下桌子,但它没有。我尝试了很多东西,但我找不到解决方案。我发布我的代码,如果有人可以帮助,我将不胜感激。谢谢。self.tableview重新加载后,应用程序连接到互联网不工作

- (void)viewDidLoad { 
[super viewDidLoad]; 
feeds = [[NSMutableArray alloc] init]; 
NSURL *url = [NSURL URLWithString:@"http://url"]; 
parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
[parser setDelegate:self]; 
[parser setShouldResolveExternalEntities:NO]; 
[parser parse]; 

Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 

self.tableView.dataSource = self; 
self.tableView.delegate = self; 

if (networkStatus == NotReachable) 
{ 

    number = 0; 

} 
else{ 

    number = 1; 
} 



if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 

    num = 5; 

} 
else{ 
    num = feeds.count; 
}} 

这些都是我调用表主要方法

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if(feeds.count > num){ 

    return num + 1; 

} 

return num; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"]; 



    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.userInteractionEnabled = YES; 
    // Set up the cell 
    [cell.textLabel setFont:[UIFont fontWithName:@"Verdana" size:15]]; 
    [cell.textLabel setNumberOfLines:2]; 
    [cell.detailTextLabel setFont:[UIFont fontWithName:@"Verdana" size:12]]; 
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"]; 

} 

return cell; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 

element = elementName; 

if ([element isEqualToString:@"item"]) { 

    item = [[NSMutableDictionary alloc] init]; 
    title = [[NSMutableString alloc] init]; 
    link = [[NSMutableString alloc] init]; 

} 

} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

if ([elementName isEqualToString:@"item"]) { 

    [item setObject:title forKey:@"title"]; 
    [item setObject:link forKey:@"link"]; 

    [feeds addObject:[item copy]]; 

} 

} 


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

if ([element isEqualToString:@"title"]) { 
    [title appendString:string]; 
} else if ([element isEqualToString:@"link"]) { 
    [link appendString:string]; 
} 

} 

- (void)parserDidEndDocument:(NSXMLParser *)parser { 

[self.tableView reloadData]; 

} 

这是我拉下来以后重装我的数据

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 

CGPoint targetPoint = *targetContentOffset; 
CGPoint currentPoint = scrollView.contentOffset; 

if (targetPoint.y > currentPoint.y) { 
    [self.tableView reloadData]; 
}} 
+0

是什么触发了从网络到饲料数组的实际加载数据? – Paulw11 2014-12-03 21:54:32

+0

这是一个RSS提要。但我在爱德华L的帮助下解决了这个问题。谢谢。 – user3882720 2014-12-03 22:17:55

回答

0

你声明实现scrollViewWillEndDragging作为实施类UIScrollViewDelegate?尝试在该事件处理程序中放置一个断点,并在下拉列表时查看您的代码是否正在执行。

编辑:您只是在第一次加载视图时更新number变量,并且只要没有网络存在就将其初始化为0。因此,你永远不会得到任何行,因为没有要显示的行。

+0

我的代码scrollViewWIllEndDraggin内部正常执行。我已经在那里推出了一个NSLog,并且还有一个突破点来看看会发生什么并且它完美运行。 – user3882720 2014-12-03 21:44:34

+0

当表重新加载时是否检查过'feeds'对象,看看它是否包含有效数据?我还注意到,没有项目的任何声明,是一个实例变量? – 2014-12-03 21:48:22

+0

如果能够连接互联网,我可以加载它们并从头开始显示它们,是否可能数据无效?但是,当我在一开始没有互联网连接打开应用程序,然后我连接它是无效的? – user3882720 2014-12-03 21:55:24

相关问题