2010-08-12 97 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() 
}) 

希望这会帮助别人。

相关问题