xCode:UITableViewCell中只有一个文本域可以正常工作

问题描述 投票:0回答:1

所以我的表视图中有3个单元格,它们都有标签名称和右侧的UITextField,用于从用户收集数据。它们的设置如下:

- (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];
}

但是,第三个单元格或messageTextField似乎是唯一遵循指令的单元格。在下面的代码中我设置了它,以便当用户在messageTextField上返回时,它将NSLog“Hello”。

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

NSLog(@"returned");

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

}

这工作正常,但如果我改变

if (messageTextField == textField) 

if (nameTextField == textField)

nameTextField是我的第一个单元格,当我返回第三个单元格时,它仍然只有NSLog“Hello”。关于为什么会发生这种情况的任何想法?

ios objective-c xcode uitableview textfield
1个回答
0
投票

问题是因为每次调用cellForRowAtIndexPath时,都会初始化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];
  }
© www.soinside.com 2019 - 2024. All rights reserved.