2012-07-17 50 views
0

我認爲這可能是一個簡單的問題,我不理解,但我花了很多時間在它,它阻止了我的工作。推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]; 

} 

任何幫助,將不勝感激。謝謝!

回答

0
- (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; 
    } 

[tableView reloadData]; // 

} 
0

這是非常困難的,告訴你的問題是什麼,正在發生的事情 - 我建議下次問一個問題少得多的隨機碼,更多的解釋,更明顯的問題(也比較好格式)。無論如何。

盡我所知,self仍然指的是原來的視圖控制器。我不知道你的代碼是來自哪個班,但如果你想要這個工作,你需要

  1. 編寫自定義類TopicsViewController
  2. 推動它的/ etc
  3. 裏面的自定義類,定義數據源,重新加載等
  4. 調用自定義類中的更改數據源方法
  5. 在自定義類的tableView上調用reloadData方法。

重點是,確保您的所有工作都發生在您的自定義表格視圖數據控制器的類內部。