2013-02-01 2 views
0

내 코드에이 문제가 있습니다. 텍스트 필드를 셀에 추가하려고합니다. 그리고 효과가있었습니다! 그러나 이후에 다시 시도했을 때는 그렇지 않았습니다. NSLog로 조금 디버그하여 셀이 nil이 아니므로 코드가 실행되지 않는다는 결론에 도달했습니다.UITableView if (cell == null) 오류

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Called1"); 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

     NSLog(@"Called?"); 
     UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
     playerTextField.adjustsFontSizeToFitWidth = YES; 
     playerTextField.textColor = [UIColor blackColor]; 
     if([indexPath row] == 0) { 
      playerTextField.placeholder = @"yoyo1"; 
      playerTextField.keyboardType = UIKeyboardTypeDefault; 
      playerTextField.returnKeyType = UIReturnKeyNext; 
     } else { 
      playerTextField.placeholder = @"yoyo2"; 
      playerTextField.keyboardType = UIKeyboardTypeDefault; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.secureTextEntry = YES; 
     } 

     playerTextField.backgroundColor = [UIColor whiteColor]; 
     playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; 
     playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     playerTextField.textAlignment = UITextAlignmentLeft; 
     playerTextField.tag = 0; 

     playerTextField.clearButtonMode = UITextFieldViewModeNever; 
     [playerTextField setEnabled: YES]; 

     [cell.contentView addSubview:playerTextField]; 

    } 


    return cell; 
} 

NSLog(@"Called?")가 호출되지 않습니다 코드,하지만 NSLog(@"Called1")는 않습니다. 왜?

감사합니다.

답변

3

그냥 약간의 변화 :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Called1"); 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    NSLog(@"Called?"); 
    //And then the rest of your cell configuration code... 
+0

일해 줘서 고마워! –

0

코드를 업데이트하면 표 셀을 초기화하는 방식이 올바르지 않습니다.

[tableView dequeueReusableCellWithIdentifier : @ "셀"]; 이 줄이 nil이 아닌 값을 반환하면 코드가 테이블 셀을 올바르게 초기화하지 않습니다. 논리는 적절한 테이블 셀을 할당 한 후 동일한 방법으로 데코 레이팅하거나 다시 사용하여 새 테이블 셀을 가져 오는 것입니다.

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{ 
     UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; 

    return cell; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     NSLog(@"Called1"); 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
     if (cell == nil) { 
      cell = [self reuseTableViewCellWithIdentifier:@"Cell" withIndexPath:indexPath]; 
     } 
     cell.accessoryType = UITableViewCellAccessoryNone; 

     NSLog(@"Called?"); 
     UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
     playerTextField.adjustsFontSizeToFitWidth = YES; 
     playerTextField.textColor = [UIColor blackColor]; 
     if([indexPath row] == 0) { 
      playerTextField.placeholder = @"yoyo1"; 
      playerTextField.keyboardType = UIKeyboardTypeDefault; 
      playerTextField.returnKeyType = UIReturnKeyNext; 
     } else { 
      playerTextField.placeholder = @"yoyo2"; 
      playerTextField.keyboardType = UIKeyboardTypeDefault; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.secureTextEntry = YES; 
     } 

     playerTextField.backgroundColor = [UIColor whiteColor]; 
     playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; 
     playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     playerTextField.textAlignment = UITextAlignmentLeft; 
     playerTextField.tag = 0; 

     playerTextField.clearButtonMode = UITextFieldViewModeNever; 
     [playerTextField setEnabled: YES]; 

     [cell.contentView addSubview:playerTextField]; 

return cell; 
} 
+0

이해가 안되네, 나는 이전에 대답을하고 반대 투표를 얻고, 같은 대답 허용 얻고 긍정적 인 표를 가져옵니다. – guenis