2012-04-12 2 views
0

저는 몇 시간 동안이 작업을 해왔고 난감 해졌습니다. 나는 아마도이 문제를 일으키는 무언가를 중복하거나 어리 석다. 그래서 내가 얻을 수있는 어떤 도움이라도 대단히 감사 할 것이다.iOS : NSXMLParser - 속성 값을 UITableView에 추가하기

나는 웹 사이트를 가져 오기 위해 NSURL을 사용하고 있습니다 : http://undignified.podbean.com/feed. 여기에서 NSXMLParser를 사용하여 XML 페이지를 구문 분석하고 elementName "인클로저"를 검색합니다. "enclosure"요소는 Mp3에 대한 URL을 포함하는 "url"속성을 포함합니다. 파싱 ​​부분이 작동하고 배열의 내용을 NS 로그 아웃 할 수 있으며 사용 가능한 모든 URL이 있는지 확인했지만 UITableView로로드 할 수 없다면 단순히 공백으로로드됩니다. _podAllEntries는 dispatch_async 내의 값으로 콘솔에 기록하지만 내 ViewController의 다른 부분은이 배열에 액세스 할 때 비어 있습니다. 비동기 백그라운드 스레드에서 대리자 일종의 아마도 다른 메서드는이 배열의 값을 액세스 할 수 있도록 구현 된 호출 된 이후 생각하십니까? 나는 완전히 틀릴지도 모른다.

RSS 피드 (XML 페이지)에 연결하려고하면 "url"이라는 속성을 구문 분석하고 값을 수집 한 다음 URL을 tableView로로드합니다. 내 코드는 다음과 같습니다. 제가 명확하지 않거나 모호한 점이 있으면 언제든지 말씀해주십시오.

UnDigParser.h - 클래스

@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> { 
} 
@property (retain) NSMutableArray *links; 
@end 

http://undignified.podbean.com/feed을 구문 분석하는 데 사용 UnDigParser.h - 구현 파일 :

#import "UnDigParser.h" 


@implementation UnDigParser 
NSMutableArray *_links; 

@synthesize links = _links; 

-(void)parserDidStartDocument:(NSXMLParser *)parser{ 
_links=[[NSMutableArray alloc] init]; 
} 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
if ([elementName isEqualToString:@"enclosure"]){ 

    NSString *link = [attributeDict objectForKey:@"url"]; 
    if (link){ 
     [_links addObject:link]; 
     } 
} 

}

-(BOOL)parse{ 
self.delegate = self; 
return [super parse]; 
} 

@end 

ViewController.h 파일 :

@interface getpodViewController : UITableViewController <UITableViewDataSource>{ 
NSMutableArray *_podAllEntries; 
NSMutableArray *_allEntries; 
} 

@property(retain) NSMutableArray *podAllEntries; 
@property(retain) NSMutableArray *allEntries; 

@end 

ViewController.M 파일 :

#import "getpodViewController.h" 
#import "PodcastEntry.h" 
#import "UnDigParser.h" 


@implementation getpodViewController 

@synthesize podAllEntries = _podAllEntries; 
@synthesize allEntries = _allEntries; 

-(void)addRows{ 
dispatch_async(dispatch_get_global_queue(0, 0), ^{ 

    NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed"]; 
     UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];  

    [parser parse]; 
    [_podAllEntries addObject:parser.links]; 
    NSLog(@"%@", _podAllEntries); 

}); 

} 

- (void)viewDidLoad { 
[super viewDidLoad]; 


self.title = @"Podcasts"; 
self.podAllEntries = [[[NSMutableArray alloc]init]autorelease]; 


[self addRows]; 
for (UnDigParser *entry in _podAllEntries){ 
    [_allEntries insertObject:entry atIndex:0]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] 
          withRowAnimation:UITableViewRowAnimationRight]; 
} 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [_podAllEntries count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell==nil){ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 

NSString *cellValue = [_podAllEntries objectAtIndex:indexPath.row]; 

cell.textLabel.text = cellValue; 

return cell; 
} 

- (void)dealloc { 
    [super dealloc]; 
[_podAllEntries release]; 
_podAllEntries = nil; 
} 

@end 
+0

이 문제는 실제로 cellForRowAtIndexPath 메소드와 관련이 있습니다. 셀을 채울 때 NSMutableArray 객체에 대해 일반 NSString을 사용하고있었습니다. 허용되지 않습니다. 다음 코드를 수정 : NSString * cellValue = [NSString stringWithFormat : @ "% @", _podAllEntries]; cell.textLabel.text = cellValue; 행을 채우지 만 모든 행을 채우지는 않습니다. 이전에 응용 프로그램이 충돌하거나 전혀 표시하지 않기 때문에 이전에는 개선 된 사항이 있습니다. 내가 곤란을 겪으면 새로운 질문 스레드를 만들 것입니다. – Fostenator

답변

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

봅니다이 추가 :)

+0

감사합니다 :). 나는 그것을 놓쳤다. 그러나 불행하게도 테이블은 아직도 공백이다. – Fostenator

+0

테이블 뷰에 대한 대리인 (2 명)을 위임자로 설정 했습니까? – Manuel

+0

아마도 그렇지 않을 수도 있습니다. 설명해 주시겠습니까? – Fostenator

0

문제는 실제로 cellForRowAtIndexPath 방법에 관한했다. 셀을 채울 때 NSMutableArray 객체에 대해 일반 NSString을 사용하고있었습니다. 허용되지 않습니다. 수정 된 코드는 다음과 같습니다.

NSString *cellValue = [NSString stringWithFormat:@"%@", _podAllEntries]; 

cell.textLabel.text = cellValue;