【问题标题】:Continuously updating UITableView issue不断更新 UITableView 问题
【发布时间】:2012-12-03 21:58:12
【问题描述】:

我有一个搜索栏,用户可以在其中搜索地址。 每次更改输入的文本时,都会调用选择器,搜索新地址并将其存储到 NSMutableArray 中。到目前为止,这工作得很好。

在搜索栏下,我有一个 UITableView,它每次都会被

调用
[searchTableView reloadData] 

将新地址添加到数组后。

一旦找到第一个地址,将其放入数组并调用 searchTableView 应用程序崩溃并显示以下消息:

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

原因:'UITableView dataSource 必须从 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; 
}

感谢您的帮助!

干杯

【问题讨论】:

  • - (NSUInteger)tableView:(UITableView *)tableVieww numberOfRowsInSection:(NSInteger)section { if (tableVieww == searchTableView) { return [list count]; } 返回 0; }
  • cellForRowAtIndexPath方法中savedTableView不应该是searchTableView吗?
  • 另外,您只使用了savedTableViewself.savedTableView。让它们都一致self.savedTableView
  • 我真的有那么笨!我怎么能监督呢?!非常感谢!

标签: iphone xcode uitableview uisearchbar updating


【解决方案1】:

错误是因为您没有在cellForRowAtIndexPath 方法中返回任何单元格。

savedTableViewcellForRowAtIndexPath 方法中应该是searchTableView


你应该有 else 条件 -> 如果(tableVieww != savedTableView) 会发生什么?

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多