2010-08-12 69 views
7

我有splitViewController它有MasterViewController一些viewController和DetailViewController一些tableViewController。當我在masterViewController上按下按鈕時,我想在detailViewController中顯示新的tableViewController而不是現有的。UITableView reloadData - cellForRowAtIndexPath未被觸發

所以,我沒有這樣的:

SearchDetailViewController *searchDetailViewController = [[SearchDetailViewController alloc] initWithStyle:UITableViewStylePlain]; 
UINavigationController *searchDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:searchDetailViewController]; 

之後,我傳遞的數據在新tableController顯示:

[searchDetailViewController passInSearchResults:listOfItems]; 

我 「推」 的新控制器splitViewController後:

[searchSplitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, searchDetailNavigationController, nil]]; 

在目標tableViewController方法「passInSearchResults」中傳遞數據,我也調用reloadDa TA。方法看起來像這樣:

- (void)passInSearchResults:(NSMutableDictionary *)searchResults { 
    self.searchResultList = searchResults; 
    NSLog(@"Data is passed in: %@",self.searchResultList); 
    [self.tableView reloadData]; 
    //[self.tableView setNeedsDisplay]; 
} 

控制檯:數據在傳遞:在這裏我得到了我想要的確切數據,似乎恰到好處。

After this I see that method "numberOfRowsInSection" is fired: 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"Setting table length: %i",[self.searchResultList count]); 
    return [self.searchResultList count]; 
} 

控制檯:設置表的長度:在這裏我得到適當的長度]

的問題是,該表中不填充傳遞的數據和方法「的cellForRowAtIndexPath」不叫。

怎麼可能是reloadData方法「numberOfRowsInSection」被激發,但不是方法「cellForRowAtIndexPath」... ???

感謝

回答

4

怎麼能是在reloadData方法 「numberOfRowsInSection」 被激發,但沒有辦法 「的cellForRowAtIndexPath」 ... ???

花了一天的時間,我覺得我們在這裏發現了一個bug。我確實發現了一個黑客/解決方法。

如果我改變在我的cellForRowAtIndexPath方法中使用的數組(增加/刪除值或完全構建它),它似乎正確地再次調用cellForRowAtIndexPath當你reloadData。

例如,在我的cellForRowAtIndexPath我有類似:

cell.textLabel.text = [[[self.searchResultsArrayWithSections objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row]; 

    return cell; 

我也有建立這個數組被調用上的viewDidLoad方法:

- (NSMutableArray *)splitIntoSectionsWithAlphabet:(NSMutableArray *)myArray 
{  
NSString *letters = @"abcdefghijklmnopqrstuvwxyz"; 
NSMutableArray *content = [NSMutableArray new]; 

//code to build array here, nothing special really 

return content; 
} 

所以我發現,如果我在調用reloadData之前在我的searchResultsArrayWithSections數組中再次調用此方法,它實際上會正確調用所有的UITableViewDelegate方法! (就像我說的,黑客但它的工作!)

//dirty hack, bug with cancel button and not realoding data, seems we have to modify this array before it will work correctly 
self.customerListArrayWithSections = [self splitIntoSectionsWithAlphabet:self.customerListArray]; 
[self.customerListTv reloadData]; 
1

我有同樣的問題,但決定是非常簡單的。首先,你可以嘗試向下滾動表格嗎? tableView:cellForRow ...應該被調用。 檢查tableView的標題'高度和contentOffset。

10

檢查,如果你是在主線程中調用時[self.tableView reloadData];

- (void)passInSearchResults:(NSMutableDictionary *)searchResults { 
if ([NSThread isMainThread]) { 
    self.searchResultList = searchResults; 
    NSLog(@"Data is passed in: %@",self.searchResultList); 
    [self.tableView reloadData]; 
} 
else { 
    [self performSelectorOnMainThread:@selector(passInSearchResults:) searchResults waitUntilDone:NO]; 
} 

}

+1

沒有必要先檢查您是否在主線程上運行。 'performSelectorOnMainThread'可以工作,即使你已經在主線程上了,就像'dispatch_async(dispatch_get_main_queue()')一樣(這可能是你在這裏實際需要的)在這裏添加'if'只會消耗不必要的處理器週期。 – 2016-11-28 23:05:53

4

檢查numberOfRows,如果是0,沒有單元格來顯示。

0

此外,您可以在viewController的init [With ...]方法中分配表,也可以在IB中連接相同的屬性。這樣可能會調用numberOf ...委託方法,而cellForRow不會。

0

我的「AutoLayouted」UITableView沒有在reloadData上調用cellForRowAtIndexPath,因爲我錯誤地設置了框架。

_tableView = [[UITableView alloc] initWithFrame:self.view.frame]; 
_tableView.translatesAutoresizingMaskIntoConstraints = NO; 

,改成CGRectZero,它的工作..

_tableView = [[UITableView alloc] initWithFrame:CGRectZero]; 
_tableView.translatesAutoresizingMaskIntoConstraints = NO; 
2

在我的情況下,問題是,reloadData()功能不是從主線程調用。

我已經把它放在一個dispatch_async()功能,解決了這個問題改變了這個:

dispatch_async(dispatch_get_main_queue(), { 
    self.tableView.reloadData() 
}) 

希望這會幫助別人。