我有一个 UITableView,它从网络服务器内的 xml 文件加载内容。
- TableView 位于标签栏控制器内;
[table reloadData]
我在里面打电话TBXMLSuccessBlock
;- 问题是 TableView 的内容没有更新。
- 我正在使用 TBXML 库;
单击按钮后,如何向 tableVIew 说 XML 文件已下载并显示?
- (IBAction)leftButton:(UIButton *)sender {
if (a<=2 && a != 0) {
a = a - 1;
NSString *leftURL = self.getDate;
[self loadURL:leftURL];
}
}
- (void)loadURL:(NSString *)newURL{
if (newURL == NULL) {
newURL = self.getDate;
}
// Create a success block to be called when the asyn request completes
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
NSLog(@"PROCESSING ASYNC CALLBACK");
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
// If TBXML found a root node, process element and iterate all children
if (tbxmlDocument.rootXMLElement)
{
// Obtain root element
TBXMLElement * root = tbxml.rootXMLElement;
if (root)
{
_storeArray = [NSMutableArray new];
TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"news" parentElement:root];
while (elem_PLANT !=nil)
{
TBXMLElement * elem_title = [TBXML childElementNamed:@"title" parentElement:elem_PLANT];
NSString *titleName = [TBXML textForElement:elem_title];
TBXMLElement * elem_artist = [TBXML childElementNamed:@"text" parentElement:elem_PLANT];
NSString *artistName = [TBXML textForElement:elem_artist];
TBXMLElement * elem_thumb = [TBXML childElementNamed:@"thumb_url" parentElement:elem_PLANT];
NSString *thumbName = [TBXML textForElement:elem_thumb];
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:@[titleName, artistName, thumbName] forKeys:@[@"title", @"text", @"thumb"]];
elem_PLANT = [TBXML nextSiblingNamed:@"news" searchFromElement:elem_PLANT];
[_storeArray addObject:dictionary];
}
}
[_tableView reloadData];
}
};
// Create a failure block that gets called if something goes wrong
TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) {
NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
};
// Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file.
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:newURL]
success:successBlock
failure:failureBlock];
}
编辑:按钮点击内的 ReloadData
- (IBAction)leftButton:(UIButton *)sender {
if (a<=2 && a != 0) {
a = a - 1;
NSString *leftURL = self.getDate;
[self loadURL:leftURL];
[_tableView reloadData];
}
}
- (IBAction)rightButton:(UIButton *)sender {
if (a<2) {
a = a + 1;
NSString *rightURL = self.getDate;
[self loadURL:rightURL];
[_tableView reloadData];
}
}
应用启动时: - 加载 1 号 URL;- 显示第 1 个 URL;当我单击一次右键时: - 加载了 2 号 URL;- 什么都没发生; 当我再右击一次时: - 加载了 3 号 URL;- 显示 2 号 URL;当我左键单击一次时: - URL 2 已加载;- 显示 URL 3;当我再单击一次时: - URL 1 已加载;- 显示 URL 2;