如何使用已解析的XML填充tableview?

时间:2014-07-02 19:50:42

标签: ios objective-c xml uitableview

您好我无法用解析的XML填充我的tableview。我正在使用XMLDictionary进行解析。

这是我的viewcontroller.h

        #import <UIKit/UIKit.h>
        #import <AVFoundation/AVFoundation.h>
        @interface KTPViewController : UIViewController <AVAudioPlayerDelegate,UITableViewDelegate, UITableViewDataSource>

{
    AVAudioPlayer *player;
    NSMutableData *_responseData;    
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *tableData;
- (IBAction)pauseTapped:(id)sender;
- (IBAction)playTapped:(id)sender;
- (IBAction)stopTapped:(id)sender;

-(void)parseXML;



@end

和我的.m文件

#import "KTPViewController.h"
    #import "XMLDictionary.h"


@interface KTPViewController ()

@end

@implementation KTPViewController
@synthesize tableView, tableData;

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self parseXML];
AVAudioSession *session =[AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
}
- (void)viewDidUnload
{
[self setTableData:nil];
[super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)pauseTapped:(id)sender {
[player pause];
}

- (IBAction)playTapped:(id)sender
{

[player setDelegate:self];
[player play];
}

- (IBAction)stopTapped:(id)sender {
if (player.playing)
{
    [player stop];
}

}


-(void)parseXML{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://podcasts.engadget.com/rss.xml"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString  = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"The string: %@", xmlString);
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(@"The dict: %@", xml);
NSDictionary *PageItem = [xml objectForKey:@"channel"];
NSArray *items = [PageItem objectForKey:@"item"];
NSLog(@"The array: %@", items);

[self setTableData:items];
NSLog(@"%@ ||||||||",tableData);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


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

}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    // Change UITableViewCellStyle
    cell = [UITableViewCell alloc];
}

// Get item from tableData
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
NSLog(@" %@", item);
// Set text on textLabel
[[cell textLabel] setText:[item objectForKey:@"channel"]];
// Set text on detailTextLabel
[[cell detailTextLabel] setText:[item objectForKey:@"link"]];

return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
@end

xml似乎被添加到tableData中,但是当我尝试将tableData添加到字典时,它不起作用,并且我的tableView上没有任何内容。

请帮助!

2 个答案:

答案 0 :(得分:1)

调用parseXml方法后尝试重新加载tableView,如下所示:

-(void)parseXML{
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://podcasts.engadget.com/rss.xml"]];
   NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   NSString *xmlString  = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
   NSLog(@"The string: %@", xmlString);
   NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
   NSLog(@"The dict: %@", xml);
   NSDictionary *PageItem = [xml objectForKey:@"channel"];
   NSArray *items = [PageItem objectForKey:@"item"];
   NSLog(@"The array: %@", items);

   [self setTableData:items];
   NSLog(@"%@ ||||||||",tableData);
   [self.tableView reloadData]; // ADD THIS LINE 
}

并且请确保您的桌面视图是否已连接委托和数据源

答案 1 :(得分:0)

我不知道为什么你需要UITableView的词典。也许你可以解释一下。在此期间,常规方法应该适用于在表上获取tableData: 确保设置self.tableView.delegate = self; self.tableView.dataSource = self;

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:identifier];
    }

    cell.textLabel.text = _tableData[indexPath.row];
    return cell;
}