【发布时间】:2013-09-07 18:46:54
【问题描述】:
我有一个带有搜索显示控制器的表格视图。当我键入搜索时,日志显示搜索正在正常工作并且过滤正常,但屏幕仅显示“无结果”。如果这有所作为,我将使用 Core Data。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = @"Cell";
if (tableView != self.tableView) {
NSLog(@"Found searchDisplayController");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(@"Found regular table View");
}
// Configure the cell...
Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = instance.name;
}
这里是 NumberOfRowsInSection 和 Number:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
【问题讨论】:
-
你在哪里更新单元格的内容?
-
就在下面:Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath]; //更多搜索数据 if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; } else { cell.textLabel.text = instance.name; }
-
@Isaac:您可以将该代码添加到您的问题中吗?这使得它更容易阅读。 -
numberOfSections和numberOfRowsInSection方法也会很有帮助。 -
您是否查看过显示“无结果”消息的逻辑并从那里返回以了解为什么它认为没有结果?
-
(格式会很好。)
标签: iphone ios uitableview core-data uisearchdisplaycontroller