2011-08-17 47 views
0

我創建了一個具有UIlabel和UITextField的自定義UITableViewCell。然後,當用戶點擊「登錄」我想從那裏得到的UITextField.text值,並將其存儲到字符串從UITableView獲取UITextField文本

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    LoginTableCell *cell = (LoginTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableCell" owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (LoginTableCell *) currentObject; 
       break; 
      } 
     }  
    } 

在我firstViewController加載和顯示自定義表。

- (IBAction)login:(id)sender { 

我知道這聽起來很簡單,但我一直在堅持這一點,似乎無法找到正確的答案來解決。

我想我可以做這樣的事情:

NSString *user = LoginTableCell.loginTextField.text; 

但這返回一個錯誤:

request for member 'text' in something not a structure or union 

大加讚賞

感謝

山姆

任何幫助
+0

而不是LoginTableCell.loginTextField.text,請使用對象名稱。例如。 currentObject.loginTextField.text – mayuur

回答

1

一般來說,你可以這樣做。確保下面的代碼中的indexPath是具有所需textField的行的正確indexPath。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
LoginTableCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
NSString *user = cell.loginTextField.text; 
0

IN的tableview didSelectRowAtIndexPath方法的委託,您必須聲明,什麼是在文本字段變量,和u必須聲明它,無論你想通過設置@property @synthesis並使用此字符串作爲全球variable.then你可以使用這個字符串,無論你想顯示,通過設置textfield.text =你的字符串;

+0

好的,但是如果在UITextField中已經​​有一些文本並且用戶在點擊登錄按鈕之前沒有選擇那個單元會怎麼樣呢? –

+0

你的答案看起來好像它依賴於用戶選擇每個單元格來存儲字符串。這可能會導致問題,我想存儲從uitextfield字符串是否用戶選擇該tableviewcell –

+0

你只想存儲從uitextfiled字符串時,用戶點擊登錄按鈕權?一個問題,你想在哪裏從uitextfield存儲這個字符串。? –