2014-02-09 1 views
0

이것은 내 첫 번째 앱이며 컨트롤을 새로 고치기 위해 끌어 오기를 구현하려고합니다. 나는 도로 블록을 쳤다. 누군가 제 코드를 확인하고 고칠 수 있습니까? 애니메이션을 새로 고침하기 위해 당기기가 작동하지만 내 테이블의 XML 데이터가 새로 고침되지 않습니다. 미리 감사드립니다.누군가 코드를 새로 고치려면 내 iOS 풀을 확인하고 수정 해 주실 수 있습니까?

#import "MasterViewController.h" 

#import "DetailViewController.h" 

@interface MasterViewController() { 
    NSXMLParser *parser; 
    NSMutableArray *feeds; 
    NSMutableDictionary *item; 
    NSMutableString *title; 
    NSMutableString *link; 
    NSString *element; 
} 
@end 

@implementation MasterViewController 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

//Paste Blog feed here 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    feeds = [[NSMutableArray alloc] init]; 
    NSURL *url = [NSURL URLWithString:@"http://Placeholder.xml"]; 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
    [parser setDelegate:self]; 
    [parser setShouldResolveExternalEntities:NO]; 
    [parser parse]; 
    UIRefreshControl *refreshControl = [UIRefreshControl new]; 
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; 
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."]; 
    self.refreshControl = refreshControl; 
    [self.tableView reloadData]; 

} 

- (void)refresh:(UIRefreshControl *)sender 
{ 
    // ... your refresh code 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://Placeholder.xml"] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 

       // handle response 
       NSXMLParser *updatedParser = [[NSXMLParser alloc] initWithData:data]; 
       [updatedParser parse]; 

       [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
      }] resume]; 
    [sender endRefreshing]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 


#pragma mark - Table View 


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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    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)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 

     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSString *string = [feeds[indexPath.row] objectForKey: @"link"]; 
     [[segue destinationViewController] setUrl:string]; 

    } 
} 


@end 

답변

1

당신은 말할 필요가 당신의 UITableView 그 자체를 새로해야한다. 현재 새 데이터를 다운로드하지만 아무 것도하지 않습니다.

테이블 뷰에서 -reloadData 메서드를 호출하거나 특정 섹션/행 범위 만로드하려는 경우 다른 재로드 메서드 중 하나를 호출 할 수 있습니다. 다운로드 한 새 데이터의 구문 분석을 완료하고 표시 할 준비가되면이 함수를 호출해야합니다.

+0

lxt, 내 코드가 어떻게 나타나야합니까? – user3251476

+1

lxt는 NSURLSession의'completionHandler'의 끝에 ('refresh :'메서드 내에서)'[self.tableView reloadData];를 추가한다고 말합니다. '[updatedParser setDelegate : self];를 추가 할 필요가있는 것 같습니다. –

+0

지속적인 도움을 주셔서 감사합니다. [self.tableView reloadData];를 추가했지만 [updatedParser setDelegate : self]를 추가하려고했을 때; 빨간색 오류가 발생했습니다. – user3251476

관련 문제