如何重新解析tableView中的xml数据?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过在 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?
解决方案显而易见又简单,但我仍然在这上面浪费了太多时间。
解决方案如下:
只需将联系人添加到数据库时放在
末尾即可。
The solution was as obvious as simple, and I still wasted way too many time on this.
Here is the solution:
Just put
at the end when you added a contact to the database.