Vote count:
0
Hi I am having trouble populating my tableview with parsed XML. I am using XMLDictionary to parse.
here is my 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
and my .m file
#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://ift.tt/hcj6jr"]];
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
The xml seems to be getting added to tableData but when i try and add tableData to a dictionary it is not working and nothing appears on my tableView.
Please Help!
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire