2017-12-29 23 views
0

したがって、私はテーブルビューに3つのセルがあり、すべてラベル名とユーザーのデータを収集するためのUITextFieldを持っています。以下のように彼らが設定されていますxCode:UITableViewCellのすべての1つのテキストフィールドだけが必要なように動作します

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 




if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
    nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    messageTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 

    nameTextField.delegate = self; 
    emailTextField.delegate = self; 
    messageTextField.delegate = self; 
cells *cells = nil; 
cells = [cellsarray objectAtIndex:indexPath.row]; 
cell.textLabel.text = cells.name; 
[cell.textLabel sizeToFit]; 
if (indexPath.row == 0) { 
    nameTextField.adjustsFontSizeToFitWidth = YES; 
        nameTextField.textColor = [UIColor blackColor]; 
        nameTextField.placeholder = @"Your name"; 
        nameTextField.keyboardType = UIKeyboardTypeDefault; 
        nameTextField.returnKeyType = UIReturnKeyGo; 
        nameTextField.backgroundColor = [UIColor whiteColor]; 
        nameTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
        nameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
        nameTextField.textAlignment = UITextAlignmentLeft; 
        nameTextField.tag = 0; 
        //playerTextField.delegate = self; 

        nameTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right 
        [nameTextField setEnabled: YES]; 
        [nameTextField addTarget:self action:@selector(textFieldDidChange1:) forControlEvents:UIControlEventEditingChanged]; 

[cell.contentView addSubview:nameTextField]; 
} 
if (indexPath.row == 1) { 
    emailTextField.adjustsFontSizeToFitWidth = YES; 
        emailTextField.textColor = [UIColor blackColor]; 
        emailTextField.placeholder = @"Your email"; 
        emailTextField.keyboardType = UIKeyboardTypeEmailAddress; 
        emailTextField.returnKeyType = UIReturnKeyGo; 
        emailTextField.backgroundColor = [UIColor whiteColor]; 
        emailTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
        emailTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
        emailTextField.textAlignment = UITextAlignmentLeft; 
        emailTextField.tag = 1; 
        //playerTextField.delegate = self; 

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



        [emailTextField addTarget:self action:@selector(textFieldDidChange2:) forControlEvents:UIControlEventEditingChanged]; 
        [cell.contentView addSubview:emailTextField]; 
} 
if (indexPath.row == 2) { 
    messageTextField.adjustsFontSizeToFitWidth = YES; 
        messageTextField.textColor = [UIColor blackColor]; 
        messageTextField.placeholder = @"Your message"; 
        messageTextField.keyboardType = UIKeyboardTypeDefault; 
        messageTextField.returnKeyType = UIReturnKeyGo; 

        messageTextField.backgroundColor = [UIColor whiteColor]; 
        messageTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
        messageTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
        messageTextField.textAlignment = UITextAlignmentLeft; 
        messageTextField.tag = 2; 
        //playerTextField.delegate = self; 

        messageTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right 
        [messageTextField setEnabled: YES]; 
        [messageTextField addTarget:self action:@selector(textFieldDidChange3:) forControlEvents:UIControlEventEditingChanged]; 
        [cell.contentView addSubview:messageTextField]; 
} 

しかし、3番目のセル、またはmessageTextFieldは、命令を次の一つだけのようです。次のコードでは、ユーザーがmessageTextFieldを返すときにNSLog "Hello"となるように設定しました。

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 

NSLog(@"returned"); 

if (messageTextField == textField) { 
NSLog(@"Hello"); 
    } 

} 

これは正常に動作しますが、私は私に戻ったとき、私は

if (messageTextField == textField) 

nameTextFieldが私の最初のセルであり、それはまだ唯一のNSLog "こんにちは" は意志

if (nameTextField == textField) 

に変更した場合3番目のセル。なぜこれが起こっているのかについてのアイデアはありますか?

+1

なぜプロトタイプセルを使用しないのですか? –

+0

あなたのcellForRowが大きすぎると複雑です。また、あなたのセルにUITextFieldを繰り返し追加しています。あなたの細胞が効果的に再利用されているかどうかわかりません –

答えて

0

問題は、cellForRowAtIndexPathが呼び出されるたびに、3つのテキストフィールドの両方を初期化するためです。 3度目にnameTextFieldemailTextFieldが作成されますが、使用されません。

だからこそ、messageTextFieldのみが機能します。以下のコードを試してみてください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier]; 




    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cells *cells = nil; 
    cells = [cellsarray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cells.name; 
    [cell.textLabel sizeToFit]; 
    if (indexPath.row == 0) { 
    nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    nameTextField.delegate = self; 
    nameTextField.adjustsFontSizeToFitWidth = YES; 
    nameTextField.textColor = [UIColor blackColor]; 
    nameTextField.placeholder = @"Your name"; 
    nameTextField.keyboardType = UIKeyboardTypeDefault; 
    nameTextField.returnKeyType = UIReturnKeyGo; 
    nameTextField.backgroundColor = [UIColor whiteColor]; 
    nameTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
    nameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
    nameTextField.textAlignment = UITextAlignmentLeft; 
    nameTextField.tag = 0; 
    //playerTextField.delegate = self; 

    nameTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right 
    [nameTextField setEnabled: YES]; 
    [nameTextField addTarget:self action:@selector(textFieldDidChange1:) forControlEvents:UIControlEventEditingChanged]; 

    [cell.contentView addSubview:nameTextField]; 
    } 
    if (indexPath.row == 1) { 
    emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    emailTextField.delegate = self; 
    emailTextField.adjustsFontSizeToFitWidth = YES; 
    emailTextField.textColor = [UIColor blackColor]; 
    emailTextField.placeholder = @"Your email"; 
    emailTextField.keyboardType = UIKeyboardTypeEmailAddress; 
    emailTextField.returnKeyType = UIReturnKeyGo; 
    emailTextField.backgroundColor = [UIColor whiteColor]; 
    emailTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
    emailTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
    emailTextField.textAlignment = UITextAlignmentLeft; 
    emailTextField.tag = 1; 
    //playerTextField.delegate = self; 

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



    [emailTextField addTarget:self action:@selector(textFieldDidChange2:) forControlEvents:UIControlEventEditingChanged]; 
    [cell.contentView addSubview:emailTextField]; 
    } 
    if (indexPath.row == 2) { 
    messageTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    messageTextField.delegate = self; 
    messageTextField.adjustsFontSizeToFitWidth = YES; 
    messageTextField.textColor = [UIColor blackColor]; 
    messageTextField.placeholder = @"Your message"; 
    messageTextField.keyboardType = UIKeyboardTypeDefault; 
    messageTextField.returnKeyType = UIReturnKeyGo; 

    messageTextField.backgroundColor = [UIColor whiteColor]; 
    messageTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
    messageTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support 
    messageTextField.textAlignment = UITextAlignmentLeft; 
    messageTextField.tag = 2; 
    //playerTextField.delegate = self; 

    messageTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right 
    [messageTextField setEnabled: YES]; 
    [messageTextField addTarget:self action:@selector(textFieldDidChange3:) forControlEvents:UIControlEventEditingChanged]; 
    [cell.contentView addSubview:messageTextField]; 
    } 
関連する問題