dequeueReusableCellWithIdentifier:对于可见单元格始终返回"nil" [英] dequeueReusableCellWithIdentifier: always returns 'nil' for visible cells
问题描述
我创建了一个自定义表格视图单元格.
I create a custom table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITextField *editField=nil;
...
NSString *CellIdentifier = [NSString stringWithFormat:@"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
switch (indexPath.row) {
case 0:
{
cell.textLabel.text=DEVNAME_TEXT_NDVC;
cell.textLabel.font=[UIFont boldSystemFontOfSize:LABEL_TEXTSIZE_NDVC];
editField=[[UITextField alloc] initWithFrame:CGRectMake(158, 9, cell.frame.size.width-183, cell.frame.size.height-15) ];
editField.tag=DEVNAME_TAG_NDVC;
...
[cell.contentView addSubview:editField ];
[editField release];
}
break;
该表只有5行,并且每行始终在屏幕上. 后来,当我尝试访问该单元格时,我总是得到"nil"
The table has 5 lines only, and each of them is on the screen always. Later, when I try to get access to the cell I always get 'nil'
下面的代码应在用户点击单元格时将光标放置在适当的UITextField上,但不会,因为'cell'始终为= 0.
The following code should place cursor to apropriate UITextField when user tap the cell, but it doesn't, since 'cell' is always =0.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *tf=nil;
[tableView deselectRowAtIndexPath: indexPath animated: YES];
[activeField resignFirstResponder]; // Last used UITextField
switch (indexPath.row) {
case 0: //
tf=(UITextField*)[cell.contentView viewWithTag:DEVNAME_TAG_NDVC];
[tf becomeFirstResponder]; // Show the keyboard
//[tf performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.7];
break;
请,您能建议出什么问题吗?为什么[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
总是= 0,
但是所有表格单元格始终可见.
Please, could you suggest what is wrong? Why [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
always =0,
but all of the table cells are always visible.
谢谢.
推荐答案
也许我听不懂这个问题,但是表格单元格仅在不再显示时才变得可重用吗?如果它们仍然可见,您如何重用它们?
Maybe I don't understand the question, but don't table cells only become reusable once they are no longer being displayed? If they are still visible, how could you reuse them?
这篇关于dequeueReusableCellWithIdentifier:对于可见单元格始终返回"nil"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!