【问题标题】:Why doesn't my UITextField, created by code, work inside my tableview Cell?为什么我的由代码创建的 UITextField 不能在我的 tableview Cell 中工作?
【发布时间】:2011-09-22 12:40:25
【问题描述】:

我想要两个单元格登录(用户和密码)。所以我运行应用程序,一切正常,但是当我单击 UITextField“playerTextField”时,程序收到了这个信号:“EXC_BAD_ACCESS”。 :(

你知道为什么吗?我已经尝试并阅读了很多不同的方法,就像这一种我更接近于获得它。

我的代码:

如果按下 Next/Done 按钮,则键盘消失。

- (BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

单元格内的我的 UITextFields。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellIdentifier = @"kCellIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:kCellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;



    if ([indexPath section] == 0) {
            playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
            playerTextField.adjustsFontSizeToFitWidth = YES;
            playerTextField.textColor = [UIColor blackColor];
            if ([indexPath row] == 0) {
                playerTextField.placeholder = @"ejemplo@gmail.com";
                playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
                playerTextField.returnKeyType = UIReturnKeyNext;
                playerTextField.delegate = self;
            }
            else {
                playerTextField.placeholder = @"Requerida";
                playerTextField.keyboardType = UIKeyboardTypeDefault;
                playerTextField.returnKeyType = UIReturnKeyDone;
                playerTextField.secureTextEntry = YES;
                playerTextField.delegate = self;
            }       
            playerTextField.backgroundColor = [UIColor whiteColor];
            playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
            playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
            playerTextField.textAlignment = UITextAlignmentLeft;
            playerTextField.tag = 0;


            playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
            [playerTextField setEnabled: YES];

            //cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0];
            cell.textLabel.font = [UIFont fontWithName:@"Futura" size:16.0];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.textLabel.shadowOffset = CGSizeMake(0.5, 0.5);
            cell.textLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:10];

            [cell.contentView addSubview:playerTextField];

            //[playerTextField release];
        }
    }
    if ([indexPath section] == 0) { // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Email";
        }
        else {
            cell.textLabel.text = @"Contraseña";
        }
    }
    else { // Login button section
        cell.textLabel.text = @"Log in";
    }
    return cell;    
}

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    “EXC_BAD_ACCESS”错误发生在您尝试访问已被释放的对象时。这意味着,在某处,对您的 UITextField 的最后保留引用被释放。

    我在可见代码中看不到任何会导致此类问题的内容。

    • alloc -- 保留 1
    • addSubView -- 保留 2
    • release(你可能应该把它重新添加进去)--retain 1

    仪器应该可以帮助您找出问题所在。有一种称为“僵尸”的仪器正是为此目的而设计的。当您到达 EXC_BAD_ACCESS 时,它将向您显示代码中保留和释放该对象的每个位置。从那里你可以找出你在哪里发布了太多次(或者可能忘记保留它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      相关资源
      最近更新 更多