2011-10-13 25 views
0

我在引用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; 
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////// 
+0

什麼時候你打算調用'resignFirstResponder'方法? –

+0

我在想我可以用完成的按鈕來切換現有的NavigationRightBarButton,或者當用戶在文本框之外輕敲時我可以調用它。大多數這些字段是文本,並有一個很好的機會有一個小數,因此鍵盤上的完成按鈕不起作用。 – TMB

+0

更新:我已經從助手類遷移到父類 – TMB

回答

1

我不知道你爲什麼,你不應該繼承的UIViewController的印象。 (幾乎所有的視圖控制器都是UIViewController的子類,如果你有幾個幾乎相同的視圖控制器,你可以從你創建的另一個視圖子類中獲益)。但是這與你的問題似乎沒有關係。

如果您繼承UITableViewCell以創建您自己的自定義單元格,您似乎在做的事情更容易完成。然後,您的「UITextFieldView」可以是子類的單元格的屬性,使其可以直接訪問,而不是使用測試循環來查找具有該類類型的子視圖。 (順便說一下,你的變量命名似乎違背了常規約定,可能使代碼有點難以閱讀,你可能想使用像「textFieldView」這樣的變量名而不是「UITextFieldView」。像傳統的變量名稱,後者看起來像一個傳統的類名稱。)

您的「助手類」可以由UITableViewCell的子類替換。網上有幾個子類化UITableViewCell的例子。如果你需要一些參考資料,讓我知道,我會發布一些鏈接(一旦我在電腦前,我可以工作:)

我希望這可以幫助,你不介意其他社論我做的評論。

+0

編輯評論非常好,很受歡迎。 – TMB

0

在的cellForRowAtIndexPath

// use local variable, not instance variable 
UITextField *textField = [[UITextField alloc] initWithFrame:TextViewFrame]; 
// tag it for later access 
textField.tag = 1; 
[cell.contentView addSubview:textField]; 
[textField release]; 

在didSelectRowAtIndexPath方法

// NOT [TableItemsHelper tableView:cellForRowAtIndexPath:] 
// that would give you a new cell 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
UITextField *textField = [cell viewWithTag:1]; 
// do what you want with textField 

「我不應該繼承一個UIViewController?」

不,這是不正確的。實際上它看起來像你的代碼是UIViewController的子類。但也許你的意思是你認爲你不應該繼承您的UIViewController的子類,如:

@interface MyViewController : UIViewController 
@interface MyViewControllerFirstVariant : MyViewController 
@interface MyViewControllerSecondVariant : MyViewController 

我不知道,我覺得你可以做,如果你想要的。但我認爲使用輔助類的設計模式更清晰。

相關問題