2011-08-17 48 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存储这个字符串。? –