gpt4 book ai didi

ios - 如何使用已解析的 XML 填充表格 View ?

转载 作者:行者123 更新时间:2023-11-29 02:50:57 25 4
gpt4 key购买 nike

您好,我在使用已解析的 XML 填充表格 View 时遇到问题。我正在使用 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 上没有任何内容。

请帮忙!

最佳答案

在调用 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
}

请确定您的 tableview 是否连接了Delegate 和 DataSource

关于ios - 如何使用已解析的 XML 填充表格 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com