使用不带基础UITableView的UISearchViewController(从情节提要)

使用不带基础UITableView的UISearchViewController(从情节提要),uitableview,storyboard,uisearchdisplaycontroller,Uitableview,Storyboard,Uisearchdisplaycontroller,我正在尝试使用UISearchDisplayController以CLGeocode作为用户类型进行搜索,以便与MKMapView一起使用。“我的视图”控制器中只有两件东西—与UISearchDisplayController关联的UISearchBar和地图视图。我用的是故事板 通常情况下,人们会使用一个底层的表视图来实现这一点,并在那里声明一个原型单元,但这里似乎没有这样做的方法,因为我没有表视图。因此,我创建了一个名为SearchTableViewCell的UITableViewCell子

我正在尝试使用UISearchDisplayController以CLGeocode作为用户类型进行搜索,以便与MKMapView一起使用。“我的视图”控制器中只有两件东西—与UISearchDisplayController关联的UISearchBar和地图视图。我用的是故事板

通常情况下,人们会使用一个底层的表视图来实现这一点,并在那里声明一个原型单元,但这里似乎没有这样做的方法,因为我没有表视图。因此,我创建了一个名为SearchTableViewCell的UITableViewCell子类,其中包含相关的nib

笔尖包含一个标签,该插座连接:

@interface SearchTableViewCell : UITableViewCell

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *textLabel;

@end
我在viewDidLoad中加载nib:

[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CellIdentifier"];
我从CLGeocoder获得的数据如下:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self.geocoder geocodeAddressString:searchString completionHandler:^(NSArray *placemarks, NSError *error) {
        self.placemarks = placemarks;
        [self.searchDisplayController.searchResultsTableView reloadData];
    }];
    return NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.placemarks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellID = @"CellIdentifier";

    SearchTableViewCell *cell = (SearchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];

    CLPlacemark *placemark = self.placemarks[indexPath.row];
    NSString *addressString = CFBridgingRelease(CFBridgingRetain(ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO)));
    cell.textLabel.text = addressString;

    return cell;
}
我的tableview代码如下所示:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self.geocoder geocodeAddressString:searchString completionHandler:^(NSArray *placemarks, NSError *error) {
        self.placemarks = placemarks;
        [self.searchDisplayController.searchResultsTableView reloadData];
    }];
    return NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.placemarks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellID = @"CellIdentifier";

    SearchTableViewCell *cell = (SearchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];

    CLPlacemark *placemark = self.placemarks[indexPath.row];
    NSString *addressString = CFBridgingRelease(CFBridgingRetain(ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO)));
    cell.textLabel.text = addressString;

    return cell;
}
搜索部分正在工作-当我到达cellforrowatinexpath时,self.placemarks中有数据。但是,第一次调用新单元时,使其出列的行失败,出现异常:

'NSUnknownKeyException', reason: '[<NSObject 0x1a840d40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key textLabel.'
我的cell对象与textLabel兼容,但我想知道错误消息中的NSObject——返回的对象不是SearchTableViewCell吗?它在IB中配置为使用正确的类。为什么出列方法要检查textLabel呢?我很确定以前我有过定制的表格单元格,没有textLabel字段


有没有关于我哪里出错的建议?

事实证明,不知何故,我有两个与textLabel的连接——一个是与IBOutlet的连接,另一个是与文件所有者的连接。这一定是NSObject返回的方式

修复后,我在-[NSLayoutConstraint constant]中得到了一个断言失败。只是一时兴起,我关闭了nib中的自动布局,这似乎不需要它和presto。现在可以了