我有一個UITableView從web服務器內的xml文件加載內容。TBXML tableview內容不會更新
- 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內部buttonClick
- (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];
}
}
當應用啓動: - URL數1被裝載; - 顯示URL編號1; 當我單擊右側一次: - 加載URL編號2; - 什麼都沒有發生; 當我再次點擊右鍵時: - 加載了URL編號3; - 顯示URL編號2; 當我點擊一次: - URL 2加載; - 顯示URL 3;當我再次點擊一次時: - 加載URL 1; - 顯示URL 2;
我確信_tableView指向我在xib中的表視圖,dataSource和委託指向File所有者和我在UIViewController類中實現。當我可以在viewDidAppear裏面的時候,reloadData可以工作,但它只是加載一次,當我點擊按鈕時它不會更新。 NSURL也是有效的。 –
Marckaraujo
2013-03-23 00:32:10
在將新數據加載到數據源中後,單擊按鈕時是否嘗試調用'reloadData'? – Isaac 2013-03-23 00:33:55
看到我的編輯,你可以看到reloadData「works」,但它更新了以前的數據。 – Marckaraujo 2013-03-23 02:35:13