如何重新解析tableView中的xml数据?

发布于 2025-01-05 04:17:05 字数 1653 浏览 1 评论 0原文

我有一个 tableView,用户可以在其中添加联系人。这些联系人被保存到 mysql 数据库中,然后返回给用户。问题是,他必须手动更新它才能获取更新的 xml。但我想在他添加联系人时执行此操作,以便立即显示。我尝试了各种方法,但总是以错误结束,或者他在表视图中多次解析数据库。我想过删除所有联系人并重新粘贴所有联系人然后更新它,但这也没有真正起作用。 这是我目前的做法:

To parse the XML:

- (void) doXMLParsing 
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Key22"] == nil) {

} else {
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.tabelle count]];

self.tableView.scrollEnabled = YES;

tabelle = [[NSMutableArray alloc] init];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
NSString *fileName = [prefs stringForKey:@"Key22"];
NSString *urlString = [NSString stringWithFormat: @"http://www.---.com/%@.xml", fileName];
NSURL *url = [NSURL URLWithString: urlString];

DataFileToObjectParser *myParser = [[DataFileToObjectParser alloc] parseXMLAtUrl:url toObject:@"Telefonbuch" parseError:nil];

for(int i = 0; i < [[myParser items] count]; i++) {
    Telefonbuch *new = [[Telefonbuch alloc] init];
    new = (Telefonbuch *) [[myParser items] objectAtIndex:i];
    [tabelle addObject:new];
    [self.tableView reloadData];
}
[XMLActivity stopAnimating];
}
}

在 viewDidLoad 中:

XMLActivity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140.0f, 290.0f, 40.0f, 40.0f)];
[XMLActivity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

[self.view addSubview:XMLActivity];

[XMLActivity startAnimating];

[self performSelector:@selector(doXMLParsing)
           withObject:nil
           afterDelay:0];
return;
tabelle = [[NSMutableArray alloc] init];

现在一切正常。我只想在添加新联系人后重新解析 XML,以便立即显示。

I have a tableView where the user can add contacts to. These contacts get saved to a mysql database and then returned to the user. Thing is, he has to update it manually in order to get the updated xml. But I wanna do it when he adds a contact so it gets shown right away. I tried various things and always ended up with either an error or he parsed the database more than once in the tableview. I thought of deleting all contacts and pasting all of them again and then update it, but this also didn't really work.
Here is how I do it at the moment:

To parse the XML:

- (void) doXMLParsing 
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Key22"] == nil) {

} else {
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.tabelle count]];

self.tableView.scrollEnabled = YES;

tabelle = [[NSMutableArray alloc] init];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
NSString *fileName = [prefs stringForKey:@"Key22"];
NSString *urlString = [NSString stringWithFormat: @"http://www.---.com/%@.xml", fileName];
NSURL *url = [NSURL URLWithString: urlString];

DataFileToObjectParser *myParser = [[DataFileToObjectParser alloc] parseXMLAtUrl:url toObject:@"Telefonbuch" parseError:nil];

for(int i = 0; i < [[myParser items] count]; i++) {
    Telefonbuch *new = [[Telefonbuch alloc] init];
    new = (Telefonbuch *) [[myParser items] objectAtIndex:i];
    [tabelle addObject:new];
    [self.tableView reloadData];
}
[XMLActivity stopAnimating];
}
}

In the viewDidLoad:

XMLActivity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140.0f, 290.0f, 40.0f, 40.0f)];
[XMLActivity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

[self.view addSubview:XMLActivity];

[XMLActivity startAnimating];

[self performSelector:@selector(doXMLParsing)
           withObject:nil
           afterDelay:0];
return;
tabelle = [[NSMutableArray alloc] init];

Everything works fine right now. I just want to reparse the XML after a new contact is added so it will get displayed right away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2025-01-12 04:17:05

您是否尝试过在 UITableView 上调用 [reloadData][1]

如果这没有回答,你能解释一下当你第二次解析 xml 时到底发生了什么吗?

Have you tried calling [reloadData][1] on your UITableView?

If this does not answer, could you explain what happens exactly when you parse xml a second time?

—━☆沉默づ 2025-01-12 04:17:05

解决方案显而易见又简单,但我仍然在这上面浪费了太多时间。
解决方案如下:

只需将联系人添加到数据库时放在

[self doXMLParsing];

末尾即可。

The solution was as obvious as simple, and I still wasted way too many time on this.
Here is the solution:

Just put

[self doXMLParsing];

at the end when you added a contact to the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文