0

我有一个具有此 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;
}

但即使数据中没有结果,它也会返回没有我的 NO RESULTS 文本的空单元格。行方法是:

- (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 中,但这不起作用。

4

3 回答 3

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你的方法也有一些错误。例如,在 case 中没有分配单元格[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;
}
于 2013-09-10T17:06:22.400 回答
0

cellForRowAtIndexPath在您正在寻找的情况下永远不会被调用。如果[self.annotationsToSort count]为 0,则它不会创建任何单元格(因此它永远不会调用cellForRowAtIndexPath)。

当它调用cellForRowAtIndexPath时有注释,所以它显然不会显示你的无结果标签。此外,您正在将您的单元格设置为nil然后尝试设置标签。因此,即使cellForRowAtIndexPath被调用也不会发生任何事情,因为您的单元格为零!(您实际上会因此而崩溃:)[self.annotationsToSort objectAtIndex:indexPath.row]

如果没有,一个很好1的解决方法是返回:tableView:numberOfRowsInSectionannotationsToSort

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;
}
于 2013-09-10T17:13:20.690 回答
0

我希望它可以帮助你。尝试添加 return 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;

}
于 2013-09-10T17:18:55.947 回答