UITableView中的搜索栏不会在单元格标签中显示文本 [英] Search Bar in UITableView doesn't display text in Cell label

查看:112
本文介绍了UITableView中的搜索栏不会在单元格标签中显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里没有发生魔术:

- (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;
}

我知道_searchResults数组填充了正确的搜索结果而且我已经适当地编辑了numberOfRowsPerSection。过滤器工作正常,但在搜索栏中输入时不会显示_searchResults [row]文本​​。如果我不使用该栏,则使用_songListArray [row]正确填充单元格。

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].

出现问题:

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

如果我不包含if表达式,我会收到错误。在搜索栏正在使用时,我应该如何初始化原型单元?我得到的只是空标签,但我知道搜索是有效的,因为如果数组中没有过滤结果,表中间会显示无结果。为什么我的songLabel.text更新?当我将标签设置为@HELLO ??时,它甚至不会显示文本而不是数组[row]。

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].

推荐答案

您需要让searchResultsTableView将单元格出列,而不仅仅是主表视图。 cellForRowAtIndexPath方法应如下所示:

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;
    }
}

在viewDidLoad中,如果使用,则应注册该类我在上面显示的标准UITableViewCell的代码。

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"];

如果您在搜索结果表中使用自定义单元格,那么您应该使用类似于I行的内容已注释掉,并注册该自定义单元格的笔尖。

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.

这篇关于UITableView中的搜索栏不会在单元格标签中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆