gpt4 book ai didi

ios - 搜索栏工作正常,但表格不显示结果

转载 作者:行者123 更新时间:2023-12-01 17:40:11 25 4
gpt4 key购买 nike

我正在尝试为我的 iOS 7 应用程序实现一个搜索栏。

当过滤器处于事件状态时,表 View 不显示值,但过滤器是正确的。

所以,我有所有的结果:
enter image description here

然后,我开始过滤没有有效结果的数据:

enter image description here

最后,我使用了一个有效的过滤器并且日志结果是正确的,但该表没有显示:
enter image description here

不知道怎么找问题。我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchResults count];
} else {
return [self.balancesData count];
}
}

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

// Configure the cell...
if (cell == nil) {
cell = [[BalancesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Balances *balance = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
balance = self.searchResults[indexPath.row];
} else {
balance = self.balancesData[indexPath.row];
}

cell.razonSocialLabel.text = balance.razonSocial;
cell.importeLabel.text = balance.importe;

tableView.backgroundColor = cell.backgroundColor = [UIColor colorWithRed: 0.937 green: 0.937 blue: 0.957 alpha: 1.0];

return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"razonSocial contains[c] %@", searchText];
self.searchResults = [self.balancesData filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex: [self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];

return YES;
}

最佳答案

@Danh 在他的评论中给出了正确的答案。这里的问题是你在说:

cell.razonSocialLabel.text = balance.razonSocial;

但是如果 cell实际上不是 BalancesTableViewCell 吗?然后 cell.razonSocialLabel为零, cell.razonSocialLabel.text调用 setText:在 nil 上,什么也没有发生。所以你得到了单元格,好吧,但它们都显示为空白。

您需要从真实表格中获取单元格;这是当您出列单元格时分发 BalancesTableViewCell 的表。但相反,您正在从 tableView 获取您的细胞。 ,在过滤表的情况下,它是搜索显示 Controller 的表 View ,它对 BalancesTableViewCell 一无所知。

因此,正如 Danh 所说,您必须更改这一行:
BalancesTableViewCell *cell = 
(BalancesTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

对此:
BalancesTableViewCell *cell = 
(BalancesTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

关于ios - 搜索栏工作正常,但表格不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com