我有一个具有此 cellForRowAtIndexPath 方法的应用程序:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = nil;
//IF NO STORES FOUND
if ([self.annotationsToSort count] == 0) {
NSLog(@"No results");
cell.nameLabel.text = @"No results";
}
// Check to see whether the normal table or search results table is being displayed
//IF ITS A SEARCH TABLE....>>>>>>>>>
if (tableView == self.searchDisplayController.searchResultsTableView) {
static NSString *CellIdentifier = @"HolidayCell";
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Location *location = [filteredResultsArray objectAtIndex:indexPath.row];
cell.nameLabel.text = location.nombrePublico;
} else {
// IF ITS A REGULAR TABLE...>>>>>>>>>>
static NSString *CellIdentifier = @"HolidayCell";
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//USING SORTED ARRAY INSTEAD OF CDfetched ARRAY
MyLocation *myLocation = [self.annotationsToSort objectAtIndex:indexPath.row];
cell.nameLabel.text = myLocation.name;
cell.dateLabel.text = myLocation.address;
cell.distancia.text = distance;
cell.phoneLabel.text = myLocation.telefono;
cell.openLabel.text = myLocation.estado;
cell.driveThruLabel.text = myLocation.driveThru;
}
return cell;
}
但即使数据中没有结果,它也会返回没有我的无结果文本的空单元格。行方法是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredResultsArray count];
} else {
//return [self.farSiman count]; WAS CRASHING
return [self.annotationsToSort count];
}
}
我已经尝试将 NO STORES FOUND 代码块放入 NORMAL STATE TABLE if/else 中,但没有成功。
最佳答案
如果 [self.annotationsToSort count]
为零,则您的 numberOfRowsInSection
方法
返回零,因此根本不会调用 cellForRowAtIndexPath
,并且返回一个空的
显示 TableView 。
如果你想显示一个特殊的单元格“没有结果”,那么你必须修改
numberOfRowsInSection
以便在这种情况下返回 1
。
例如,(未经测试,只是为了给您提供思路):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self.annotationsToSort count] == 0) {
return 1;
} else if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredResultsArray count];
} else {
return [self.annotationsToSort count];
}
}
您的 cellForRowAtIndexPath
方法也有一些错误。例如,
在 [self.annotationsToSort count] == 0
的情况下没有分配单元格。
它应该看起来更像这样(同样未经测试):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"HolidayCell";
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//IF NO STORES FOUND
if ([self.annotationsToSort count] == 0) {
NSLog(@"No results");
cell.nameLabel.text = @"No results";
} else if (tableView == self.searchDisplayController.searchResultsTableView) {
Location *location = [filteredResultsArray objectAtIndex:indexPath.row];
cell.nameLabel.text = location.nombrePublico;
} else {
MyLocation *myLocation = [self.annotationsToSort objectAtIndex:indexPath.row];
cell.nameLabel.text = myLocation.name;
cell.dateLabel.text = myLocation.address;
cell.distancia.text = distance;
cell.phoneLabel.text = myLocation.telefono;
cell.openLabel.text = myLocation.estado;
cell.driveThruLabel.text = myLocation.driveThru;
}
return cell;
}
关于ios - 为什么我的 NO RESULTS 单元格没有显示在我的 TableView Controller 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724877/