Search Bar in UITableView doesn't display text in Cell label

微笑、不失礼 提交于 2019-12-02 15:08:59

问题


Here's where the magic isn't happening:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
static NSString *CellIdentifier = @"songCell";
songViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...

long row = indexPath.row;

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

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.songLabel.text = _searchResults[row];
} else {
    cell.songLabel.text = _songListArray[row];
}

return cell;
}

I know the _searchResults array is populated with the correct search results and I've edited the numberOfRowsPerSection appropriately. The filter is working correctly, but it won't display the _searchResults[row] text while typing into the search bar. If I don't use the bar, the cells are populated correctly with _songListArray[row].

Something is going wrong in:

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

If I don't include the if expression, I get an error. How should I initialize the prototype cell while the search bar is in use? All I get is empty labels, but I know the search is working because if there are no filtered results in the array the table says "No Results" in the middle. Why isn't my songLabel.text updating?? It won't even display text when I set the label to @"HELLO??" instead of the array[row].


回答1:


You need to have the searchResultsTableView dequeue a cell, not just your main table view. The cellForRowAtIndexPath method should look something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        RDTriangleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[indexPath.row];
        return cell;
    }else{
        UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
        //RDCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
        cell.textLabel.text = self.filteredData[indexPath.row];
        return cell;
    }
}

In viewDidLoad, you should register the class if you use the code I show above for a standard UITableViewCell.

[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SearchCell"];

If you use a custom cell for the search results table, then you should use something like the line I have commented out, and register the nib for that custom cell.



来源:https://stackoverflow.com/questions/20809863/search-bar-in-uitableview-doesnt-display-text-in-cell-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!