2012-12-03 41 views
0

我有一个用户搜索地址的搜索栏。 每次输入的文本被改变时,一个选择器被调用,新的地址被搜索并存储到NSMutableArray中。这到目前为止完美。不断更新UITableView问题

在搜索栏我有一个是由

[searchTableView reloadData] 

称为每当一个新的地址被添加到阵列中后的UITableView。

一旦第一地址被找到,放入阵列和searchTableView被调用下面的消息应用程序崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 

原因:“的UITableView数据源必须从的tableView返回一个细胞:的cellForRowAtIndexPath: “

这里是我使用的代码:

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

     list = [[NSMutableArray alloc]init]; 

[geoCoder geocodeAddressString:self.addressSearch.text 
      completionHandler:^(NSArray* placemarks, NSError* error){ 
       for (CLPlacemark* aPlacemark in placemarks) 
       { 

        for (int i=0; i<[placemarks count]; i++) { 

         NSString *temp = [NSString stringWithFormat:@"%@",[[[[placemarks objectAtIndex:i]addressDictionary] valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]]; 

         [list addObject:temp]; 

         [searchTableView reloadData]; 


         NSLog(@"List: %@", list); 
        } 

       } 
      }]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableVieww cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

... 

if (tableVieww == savedTableView) { 

    UIView *bgColor; 

    cell = [self.savedTableView dequeueReusableCellWithIdentifier:[list objectAtIndex:indexPath.row]]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[list objectAtIndex:indexPath.row]]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

     bgColor = [[UIView alloc] initWithFrame:cell.frame]; 
     [cell addSubview:bgColor]; 
     [cell sendSubviewToBack:bgColor]; 

    } 

    cell.textLabel.text = [list objectAtIndex:indexPath.row]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 

    cell.textLabel.textColor = [UIColor whiteColor]; 


    if (indexPath.row % 2 == 0){ 
     bgColor.backgroundColor = [UIColor colorWithRed:098.0f/255.0f 
                green:211.0f/255.0f 
                blue:182.0f/255.0f alpha:0.90f]; 
    } else { 
     bgColor.backgroundColor = [UIColor colorWithRed:230.0f/255.0f 
                green:089.0f/255.0f 
                blue:128.0f/255.0f alpha:0.90f]; 
    } 

} 

return cell; 
} 

感谢您的帮助!

干杯

+0

- (NSUInteger)的tableView:(UITableView的*)tableVieww numberOfRowsInSection:(NSInteger的)部分 { 如果(tableVieww == searchTableView){ 返回[列表计数]; } return 0; } – freshking

+0

不应该'savedTableView'是'cellForRowAtIndexPath'方法中的'searchTableView'? – user427969

+0

此外,您只使用'savedTableView'和'self.savedTableView'。让他们一致'self.savedTableView' – user427969

回答

0

的错误是因为你没有在cellForRowAtIndexPath方法返回的任何细胞。

savedTableView应该是searchTableViewcellForRowAtIndexPath方法。


您应该还有其他条件 - >如果(tableVieww != savedTableView)会发生什么情况?

- (UITableViewCell *)tableView:(UITableView *)tableVieww cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (tableVieww == savedTableView) { 
     .... 
    } else { 
     // Initialize cell here as well 
    } 
}