0

我的代码有这个问题,我正在尝试将文本字段添加到我的单元格中。它奏效了!但是当我后来再次尝试时,它没有。我用 NSLog 进行了一些调试,得出的结论是 cell 不是 = nil,因此我的代码没有运行。

-(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")会。为什么?

谢谢。

4

2 回答 2

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...
于 2013-02-01T21:05:13.887 回答
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;
}
于 2013-02-01T20:55:51.263 回答