2012-10-29 29 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中,不在其他三种方法中。希望这可以帮助。