2012-10-29 185 views
2

我正在設置一個設置屏幕,您可以在其中通過uisearchbar選擇工作站。我有一個分段的tableview,一個站的第一個字母作爲標題,每個站都按它的第一個字母分類。到現在爲止還挺好。 我哈貝2 NSMutableArray的,每節,電臺。一個是未過濾的數組(當我沒有過濾時我使用它),另一個是當我搜索某些東西時。 (我通過謂詞來做到這一點)。在鍵盤上的每個按鍵上,我執行一個[self.tableView reloadData];這工作,但滾動視圖停留太久!因此,您可以滾動瀏覽選定數組中實際有多少結果。這會導致崩潰,因爲它試圖獲取不存在的對象。TableView無法正確加載

所以它看起來像tableview不計數陣列權或東西? 有沒有人熟悉這個問題?

下面是一些代碼:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (self.searching) { 
     return [self.tableFilterd count]; 
    } else { 
     return [self.tableData count]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"Rows for section"); 
    // Return the number of rows in the section. 
    if (self.searching) { 
     NSLog(@"Editing section: %i, count %i", section, [[self.tableFilterd objectAtIndex:section] count]); 
     return [[self.tableFilterd objectAtIndex:section] count]; 
    } else { 
     NSLog(@"Not editing"); 
     return [[self.tableData objectAtIndex:section] count]; 
    } 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    SettingsHeaderCell *cell = [[[SettingsHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HeaderCell"] autorelease]; 

    cell.labelLetter.text = [[self.tableLetters objectAtIndex:section] capitalizedString]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 40; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 52; 
} 

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    SettingsCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[SettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if (self.searching) { 
     StationObject *object = (StationObject *)[[self.tableFilterd objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
     [cell setStationObject:object]; 
    } else { 
     StationObject *object = (StationObject *)[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
     [cell setStationObject:object]; 
    } 

    return cell; 
} 
+0

您正在使用NSArray填充tableView。其實你添加或刪除nsarray中的項目一次,如果你添加。所以你的數組總是相同的,我認爲它沒有被過濾掉。嘗試使用nsmutablearray,如果你更新你的數組無論如何.. –

+0

哦,不,這是一個NSMutableArray –

+0

你可以發佈代碼,你在哪裏填充你過濾陣列 –

回答

0

您現在可能已經解決了這一點,但我懷疑你是不是排空或者陣列。在方法中:

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText 
{ 

//Remove all objects first 
[self.tableFiltered removeAllObjects]; 
[self.tableData removeAllObjects]; 

另外你只需要調用[self.tableView reloadData];在textDidChange中,不在其他三種方法中。希望這可以幫助。