2013-02-01 65 views
0

我有我的代码这个问题,我想添加文本域到我的单元格。它的工作!但是当我以后再试时它没有。我用NSLog调试了一下,得出的结论是cell不是= nil,因此我的代码没有运行。UITableView如果(单元格==空)错误

-(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:@“Cell”];如果此行返回非零,则代码将无法正确初始化表格单元格。逻辑是通过在分配合适的表格单元格后以相同的方式对其进行装饰或重新使用来获取新的表格单元格。

-(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

相关问题