2011-09-27 29 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」; 
相關問題