我認爲這可能是一個簡單的問題,我不理解,但我花了很多時間在它,它阻止了我的工作。推UITableViewControllers和篩選/重新加載數據
我有一個顯示4行「主題」的UITableViewController。當你觸摸它時,它將創建我的UITableViewController類的一個實例,將數據源數組設置爲「子主題」數組並將控制器壓入導航堆棧。當我進入我的「全部」主題頁面時,如果有10行,我有一個分段控件來過濾結果。我能夠成功地過濾數組,但是當我將它分配給self.topicsDataSource並調用[self.tableView reloadData]時,沒有任何反應。委託方法numberOfSections和numberOfRowsInSection,但cellForRowAtIndexPath不是。當我按下導航後退按鈕時,它會返回到上一個表格,現在我要在頂部表格中篩選結果。
所以這就是我認爲正在發生的事情。 1.我實際上改變了上一張表的數據源,並且在該表上調用了self.tableView.reloadData。 2. cellForRowAtIndexPath不會被調用,因爲當時重新加載的tableview不可見。
這裏是我正在實例化推送表視圖。此類稱爲TopicsViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *idtopic = [[self.topicsDataSource objectAtIndex:indexPath.row] idtopic];
NSArray *subtopics = [DataManager getSubtopicsForTopic:idtopic];
if ([subtopics count] > 0) {
TopicsViewController *topicsVC = [[TopicsViewController alloc ] init];
topicsVC.tableView.rowHeight = 106;
topicsVC.parentTopicId = idtopic;
topicsVC.topicsDataSource = [NSMutableArray arrayWithArray:subtopics];
topicsVC.diseaseStateId = self.diseaseStateId;
topicsVC.title = [[self.topicsDataSource objectAtIndex:indexPath.row] topic];
[self.navigationController pushViewController:topicsVC animated:YES];
}
else if (![[self.topicsDataSource objectAtIndex:indexPath.row] topic]) {
//all topics was selected
TopicsViewController *topicsVC = [[TopicsViewController alloc] initWithNibName:nil bundle:nil];
topicsVC.tableView.rowHeight = 106;
NSMutableArray *mArray = [[NSMutableArray alloc] initWithArray:[DataManager getTopicsAtEndOfTree]];
topicsVC.allTopics = mArray;
topicsVC.topicsDataSource = topicsVC.allTopics;
topicsVC.diseaseStateId = self.diseaseStateId;
topicsVC.parentTopicId = [NSNumber numberWithInt:100];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:[[NSArray alloc] initWithObjects:@"All", @"Speaker Direct", @"Live Meeting", nil]];
[segControl addTarget:self action:@selector(segControlChanged:) forControlEvents:UIControlEventValueChanged];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setSelectedSegmentIndex:0];
topicsVC.navigationItem.titleView = segControl;
topicsVC.tableView.dataSource = topicsVC;
[self.navigationController pushViewController:topicsVC animated:YES];
}....
和這裏就是我過濾數據和重裝的tableView:
- (void)segControlChanged:(UISegmentedControl *)sender
{
self.allTopics = [NSMutableArray arrayWithArray:[DataManager getTopicsAtEndOfTree]];
if (sender.selectedSegmentIndex == 1) {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"type == 2"];
NSMutableArray *mArr = [[NSMutableArray alloc] initWithArray:[self.allTopics filteredArrayUsingPredicate:pred]];
self.topicsDataSource = mArr;
}
else if (sender.selectedSegmentIndex == 2) {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"type == 1"];
NSMutableArray *mArr = [[NSMutableArray alloc] initWithArray:[self.allTopics filteredArrayUsingPredicate:pred]];
self.topicsDataSource = mArr;
}
else {
NSMutableArray *mArr = [[NSMutableArray alloc] initWithArray:[DataManager getTopicsAtEndOfTree]];
self.topicsDataSource = mArr;
}
[self.tableView reloadData];
}
任何幫助,將不勝感激。謝謝!