我在引用UITextField時遇到了問題。現在我正在使用UIAlertView來驗證參考。在這個應用程序中,我有幾個視圖非常相似,並且構建了一個HelperClass(我不應該繼承UIViewController?)我擁有的視圖控制器將委託消息傳遞給TableItemsHelperClass。在該TableItemsHelperClass我動態地添加單元格到表。現在我遇到了引用這些單元格的問題,我的最終目標是處理釋放第一響應者(鍵盤),但會很高興抓住Textfields內容來驗證參考。如何引用一個動態添加到UITableView的UITextField?
更新 我從使用助手類到子類的UIViewController重新分解。某些變量名稱已更改。這兩種方法都可以在父類中找到。我現在有這樣的情況,如果
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