我有一个具有此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中,如果/ else并且它没有用。
答案 0 :(得分:2)
如果[self.annotationsToSort count]
为零,则为numberOfRowsInSection
方法
返回零,因此根本不调用cellForRowAtIndexPath
,并且为空
显示表格视图。
如果要显示特殊单元格“无结果”,则必须进行修改
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;
}
答案 1 :(得分:0)
cellForRowAtIndexPath
。如果[self.annotationsToSort count]
为0,那么它将不会创建任何单元格(因此它永远不会调用cellForRowAtIndexPath
)。
当它调用cellForRowAtIndexPath
时会有注释,所以它显然不会显示你的无结果标签。您还要将单元格设置为nil
,然后尝试设置标签。因此,即使调用了cellForRowAtIndexPath
,也不会发生任何事情,因为你的细胞是零! (你实际上会因此而崩溃:[self.annotationsToSort objectAtIndex:indexPath.row]
)。
如果没有1
那么,tableView:numberOfRowsInSection
中的annotationsToSort
会返回if (![self.annotationsToSort count]) {
return 1;
}
,这样做很好:
cellForRowAtIndexPath
然后在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = nil;
// Check to see whether the normal table or search results table is being displayed
if (tableView == self.searchDisplayController.searchResultsTableView) {
// your code //
} else {
static NSString *CellIdentifier = @"HolidayCell";
// you need to dequeue the cell first, then check if it is nil!
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// other static cell configurations
}
if ([self.annotationsToSort count] == 0) {
NSLog(@"No results");
cell.nameLabel.text = @"No results";
} else {
// this line will crash your code if annotationsToSort is empty
MyLocation *myLocation = [self.annotationsToSort objectAtIndex:indexPath.row];
// configure cell with your code //
}
}
return cell;
}
:
{{1}}
答案 2 :(得分:0)
我希望它可以帮到你。尝试添加返回1;按照代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
if ([self.annotationsToSort count] == 0)
{
return 1;
}
else
{
return [self.annotationsToSort count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//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;
}