UITextField를 참조하는 데 문제가 있습니다. 지금은 참조를 확인하기 위해 UIAlertView를 사용하고 있습니다. 이 응용 프로그램에서 나는 매우 유사해야하고 HelperClass (나는 UIViewController를 서브 클래 싱하지 않아도 되는가?)를 만들 수있는 몇 가지 뷰가 있습니다. ViewItem은 Delegate 메시지를 TableItemsHelperClass에 전달하고 있습니다. 해당 TableItemsHelperClass 동적으로 테이블에 셀을 추가하고 있습니다. 이제 해당 셀을 참조하는 문제가 발생했습니다. 내 목표는 첫 번째 응답자 (키보드)를 릴리스하는 것이지만, 참조를 확인하기 위해 Textfields 콘텐츠를 가져 오는 것이 좋습니다.동적으로 UITableView에 추가 된 UITextField를 어떻게 참조합니까?
업데이트는 나는있는 UIViewController 서브 클래 싱하기 위해 HelperClass를 사용 다시 고려했다. 일부 변수 이름이 변경되었습니다. 이 두 가지 메소드는 모두 상위 클래스에서 찾을 수 있습니다.
tableView:didSelectRowAtIndexPath:
이tableView:cellForRowAtIndexPath:
이라면is nil
이 기록됩니다. 왜 내가 그 세포를 돌려주지 않는거야?
//tableView cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell != nil) {
NSLog(@"is not nil");
return cell;
}
NSLog(@"is nil");
CGRect CellFrame = CGRectMake(0, 0, 300, 65);
CGRect LabelViewFrame = CGRectMake(5, 5, 290, 25);
CGRect TextViewFrame = CGRectMake(5, 35, 290, 25);
cell = [[[UITableViewCell alloc] initWithFrame:CellFrame
reuseIdentifier:CellIdentifier] autorelease];
textFieldView = [[UITextField alloc] initWithFrame:TextViewFrame];
labelView = [[UILabel alloc] initWithFrame:LabelViewFrame];
textFieldView.backgroundColor = [UIColor greenColor];
labelView.backgroundColor = [UIColor redColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (@"int" == [self.taxPayerTypes objectAtIndex:indexPath.row]) {
textFieldView.keyboardType = UIKeyboardTypeDecimalPad;
}
if (0 == indexPath.section) {
[labelView setText:[self.taxPayerList objectAtIndex:indexPath.row]];
} else {
[labelView setText:[self.spouseList objectAtIndex:indexPath.row]];
}
[cell.contentView addSubview:textFieldView];
[cell.contentView addSubview:labelView];
return cell;
}
//tableView didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *i in [[[self tableView:tableView cellForRowAtIndexPath:indexPath] contentView] subviews]) {
if ([i isKindOfClass:[UITextField class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSStringFromClass([i class])
message:[i text]//[(UITextField*)i text]
delegate:self
cancelButtonTitle:@"a"
otherButtonTitles:@"b", nil];
[alert show];
[alert release];
}
}
}
원래
//in the UIViewController/////////////////////////////////////////////////////////////////////////////////
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return [TableItemsHelper tableView:tableView cellForRowAtIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *i in [[[TableItemsHelper tableView:tableView cellForRowAtIndexPath:indexPath] contentView] subviews]) {
if ([i isKindOfClass:[UITextField class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSStringFromClass([i class])
message:[i text]
delegate:self
cancelButtonTitle:@"button 1"
otherButtonTitles: @"button", nil];
[alert show];
[alert release];
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//In the TableItemsHelperClass//////////////////////////////////////////////////////////////////////////
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CGRect CellFrame = CGRectMake(0, 0, 300, 65);
CGRect TextViewFrame = CGRectMake(5, 35, 290, 25);
// …
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame
reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
UITextFieldView = [[UITextField alloc] initWithFrame:TextViewFrame];
[cell.contentView addSubview:UITextFieldView];
return cell;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
정확하게'resignFirstResponder' 메서드를 호출 할 예정입니까? –
기존 버튼을 사용하여 기존 NavigationRightBarButton을 전환 할 수 있다고 생각하거나 사용자가 텍스트 필드 외부를 누를 때 호출 할 수 있다고 생각했습니다. 이 필드의 대부분은 텍스트이고 십진수를 가질 확률이 높으므로 키보드의 완료 버튼이 작동하지 않습니다. – TMB
업데이트 : 상위 클래스로 도우미 클래스에서 마이그레이션했습니다. – TMB