그래서 테이블보기에 세 개의 셀에 레이블 이름이 있고 UITextField에 사용자의 데이터를 수집하는 셀이 있습니다. 그들은 다음과 같이 설정됩니다 :xCode : UITableViewCell에있는 모든 텍스트 필드 중 하나만이 원하는대로 작동합니다.
- (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)
에 나는에 돌아올 때 nameTextField, 그것은 여전히 것 NSLog "안녕하세요"내 첫 번째 셀입니다
if (nameTextField == textField)
내 세 번째 셀. 왜 이런 일이 일어나고 있는지에 대한 아이디어가 있습니까?
왜 프로토 타입 셀을 사용하지 않습니까? –
귀하의 cellForRow가 너무 크고 복잡합니다. 또한 UITextField를 셀에 반복해서 추가하고 있습니다. 셀을 효과적으로 재사용하고 있는지 잘 모르겠습니다. –