xcode 使用搜索显示控制器时自定义 CellIdentifier 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10189243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom CellIdentifier is Null When Using Search Display Controller
提问by kubilay
In my tableview have custom cells that I initialize from a UITableViewCell class. I have sections for first letters of records and have an indexPath that is being created dynamically.
在我的 tableview 中有我从 UITableViewCell 类初始化的自定义单元格。我有记录第一个字母的部分,并且有一个动态创建的 indexPath。
I wanted to add a search display controller to my tableview. So I did, created all methods to filter data. I am sure that my functions are working well because I am printing array count to screen for search results.
我想在我的 tableview 中添加一个搜索显示控制器。所以我做了,创建了所有过滤数据的方法。我确信我的函数运行良好,因为我正在打印数组计数以筛选搜索结果。
My problem is that the first time view loads, the data is on the screen. But when I hit the search input and type a letter, than I get 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
error. After I used a breakpoint I saw that my custom cell is nil after searching. Data is exist, but cell is not being initialized.
我的问题是第一次加载视图时,数据在屏幕上。但是当我点击搜索输入并输入一个字母时,我得到了'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
错误。使用断点后,我看到我的自定义单元格在搜索后为零。数据存在,但单元格未初始化。
Here is the code I use for custom cell initializing:
这是我用于自定义单元格初始化的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ObjectCell";
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.label1.text = [myObject objectForKey:@"myValue"];
return cell;
}
I believe I made a mistake when putting controls in IB. So I added screenshots of objects:
我相信在 IB 中放置控件时我犯了一个错误。所以我添加了对象的截图:
Connections inspector for my table view
我的表视图的连接检查器
Connections inspector for my search display controller
我的搜索显示控制器的连接检查器
EDIT: Problem is actually solved, I have used a UISearchBar instead of Search Display Controller but I guess this issue remains unsolved. So I'm willing to try any ways to make it work.
编辑:问题实际上已解决,我使用了 UISearchBar 而不是搜索显示控制器,但我想这个问题仍未解决。所以我愿意尝试任何方法让它发挥作用。
回答by nikravi
As of here search display controller question,
you need to access the self.tableView
instead of tableView
:
截至此处搜索显示控制器问题,您需要访问self.tableView
而不是tableView
:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CellId"];
// do your thing
return cell;
}
回答by FilmJ
For those using iOS 5 and StoryBoards, you would want to use the following method instead of initWithIdentifier:
对于使用 iOS 5 和 StoryBoards 的用户,您可能需要使用以下方法而不是initWithIdentifier:
initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
Example:
例子:
NSString *cellIdentifier = @"ListItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
回答by phbardon
I had the same issue, with custom cells (built in Storyboard) not being drawn as soon as the first letter was put in the search field. The search was successful however.
我遇到了同样的问题,在搜索字段中放入第一个字母后,不会立即绘制自定义单元格(内置在 Storyboard 中)。不过搜索还是成功的。
Finally I found a good tutorial from Brenna Blackwellsuggesting to configure manually the cell drawing in the corresponding subclass of UITableViewCell, adding UILabels and other items.
最后找到了Brenna Blackwell的一篇很好的教程,建议在UITableViewCell对应的子类中手动配置cell绘图,添加UILabels等项。
回答by rckoenes
I'm not sure about how this should work in storeboarding.
我不确定这在 storeboarding 中应该如何工作。
But normally you would check if the [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
returns a cell.
Because if the cell in not loaded before or there aren't any cells to reuse you will have to create a new cell:
但通常你会检查是否[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
返回一个单元格。因为如果之前未加载单元格或没有任何可重用的单元格,您将不得不创建一个新单元格:
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}
Also when in declaring local variables in Objective-C we tent not to capitalize the first letter.
同样,在 Objective-C 中声明局部变量时,我们尽量不将第一个字母大写。