2011-09-27 66 views
3

我创建了一个自定义表格视图单元格。dequeueReusableCellWithIdentifier:总是针对可见单元格返回'nil'

- (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行,并且它们每个都始终在屏幕上。 后来,当我尝试访问单元格时,我总是得到'零'

下面的代码应该将光标放在适当的UITextField当用户点击单元格,但它不会,因为'单元格'总是= 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, 但所有表格单元始终可见。

谢谢。

+0

我也有一个问题:的tableView dequeueReusableCellWithIdentifier:CellIdentifier]上始终= 0一些按钮,点击 – Shamsiddin

回答

4

也许我不明白这个问题,但是表格单元格只有在它们不再被显示时才会变得可重用?如果它们仍然可见,那么您如何重用它们?

+0

哼! +1好点! – EmptyStack

1

是的,dequeueReusableCellWithIdentifier:总是返回nil除了使用registerNib:forCellReuseIdentifier:

3

更改此:

NSString *CellIdentifier = [NSString stringWithFormat:@"cell:%d",indexPath.row]; 

到:

static NSString *CellIdentifier = @“XXXX”; 
相关问题