2012-05-28 4 views
0

Ben Reeves가 HTMLParser를 사용 중입니다. 그것은 위대한 작품이지만 유일한 문제는 내가 UITableView에 출력을 넣을 수 없다는 것입니다. 누구든지이 코드의 문제점을 알 수 있습니까? .................................................. .................................UITableView에서 파싱 된 결과

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 

    NSError *error = nil; 
    NSURL *url=[[NSURL alloc] initWithString:@"http://website.com/"]; 
    NSString *strin=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 

    HTMLParser *parser = [[HTMLParser alloc] initWithString:strin error:&error]; 

    if (error) { 
     NSLog(@"Error: %@", error); 
     return; 
    } 

    HTMLNode *bodyNode = [parser body]; 
    NSArray *divNodes = [bodyNode findChildTags:@"div"]; 

    for (HTMLNode *inputNode in divNodes) { 
     if ([[inputNode getAttributeNamed:@"class"] isEqualToString:@"views-field-title"]) { 
      NSLog(@"%@", [inputNode allContents]); 

      listData = [[NSArray alloc] initWithObjects:[inputNode allContents], nil]; 
     } 
    }  
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 

    return cell; 
} 

@end 
+0

사용할 수 있습니까? HTML 데이터를 테이블에 어떻게 표시 하시겠습니까? –

+0

UITableView 이벤트 목록을 갖고 싶어하지만 tableview는 배열의 마지막 하나만 보여줍니다. 나는 [inputNode allContents]를 배열로 취급하고 그것을 "cell.textLabel.text = [listData objectAtIndex : row]"에 넣으려고합니다. 콘솔에서는 이벤트 목록을 표시하지만 셀의 마지막 이벤트 만 표시합니다. 나는 뭔가를 놓칠지도 모른다. 무엇을 모른다 ... – Benjamen

답변

0

찾을 때마다 배열이 다시 초기화됩니다. 새로운 요소. 나는 당신이 NSMutableArray 그래서 당신이 그것에 데이터를 추가 할 수 있어야한다

listData = [[NSMutableArray alloc] init];

listData에 루프의

listData = [[NSArray alloc] initWithObjects:[inputNode allContents], nil];

외부를 이동하고 변경할 필요가 있다고 생각합니다. 변수 정의에서도이 값을 변경해야합니다. 루프 내부에 그런

, 당신은 당신이 일이 원하는에 관해서는 조금 명확하게 할 수 [listData addObject:[inputNode allContents]];

+0

그것은 일했다 !!!. 정말 고맙습니다. – Benjamen

+0

@ Benjamen 우수. 그것을 듣고 기뻐! :) –

관련 문제