【问题标题】:xCode: Only one textfield of all in UITableViewCell works as wantedxCode:UITableViewCell 中只有一个文本字段可以按需要工作
【发布时间】:2017-12-29 08:12:27
【问题描述】:

所以我的表格视图中有 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”。关于为什么会发生这种情况的任何想法?

【问题讨论】:

  • 为什么不使用原型单元?
  • 您的 cellForRow 太大太复杂,而且您一遍又一遍地向单元格添加 UITextField,我不知道您是否有效地重用了单元格

标签: ios objective-c xcode uitableview textfield


【解决方案1】:

问题是因为每次调用cellForRowAtIndexPath 时,都会初始化两个文本字段。在第三次调用时,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];
  }

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 2015-05-01
    • 2021-07-29
    • 2019-05-06
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2015-08-18
    相关资源
    最近更新 更多