2014-12-03 59 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

相關問題