tableView:cellForRowAtIndexPath:使用搜索栏时不会被调用

时间:2010-11-04 15:44:35

标签: iphone objective-c

我有一个带有nib的viewcontroller - 里面有tableview,searchbar和searchdisplaycontroller。

searchdisplaycontroller的contentscontroller,结果委托,委托和结果数据源连接到IB中的viewcontroller。搜索栏的委托连接到IB中的视图控制器。主表视图也在运行时正确连接到viewcontroller及其加载项。

除了resultstableview之外,其他一切似乎都有效 - 它永远不会被填充,并且tableView:cellForRowAtIndexPath:在用户输入搜索栏之后永远不会被调用 - 它只会被调用以填充主表视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell o f the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if (tableView.tag == 2)
    {
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        // Configure the cell.
        NSHotel *selectedHotel = [[self hotelList] objectAtIndex:[indexPath row]];

        //Store the hotel id for use later
        selectedHotel.id = [indexPath row];

        //cell.textLabel.text = [NSString stringWithFormat:@"Catalonia Majorica", indexPath.row];
        cell.textLabel.text = [selectedHotel name];
    }
    else if (tableView.tag == 1)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        // Configure the cell...    
        NSAirport *selectedAirport = nil;

        if (tableView == self.searchDisplayController.searchResultsTableView)
        {
            selectedAirport = [self.filteredListContent objectAtIndex:indexPath.row];
        }
        else
        {
            selectedAirport = [self.listContent objectAtIndex:indexPath.row];
        }

        //Set the label of the cell formatting the distance from the device 
        cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@ miles", 
                               selectedAirport.name, [self formattedStringWithDecimal:selectedAirport.milesFromDevice]];

        //Adjust the front of the label
        cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

修正了它 - 它在这里返回0:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        if (tableView == self.searchDisplayController.searchResultsTableView)
        {
            return [self.filteredListContent count];
        }
        else if (tableView.tag == 1)
        {
            return [self.listContent count];
        }
        else if (tableView.tag == 2)
        {
            // Return the number of rows in the section.
            return [[self hotelList] count];
        }

        NSLog(@"The rows in tableview!");
        return 0;
    }