2010-08-14 171 views
0

我在创建表单并想知道是否有人知道如何从表格单元格中检索UITextField值。访问表单元格中的数据

- (IBAction)saveForm:(id)sender { 
    NSLog(@"TextField Value => %@", titleField.text); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ... 
switch(indexPath.section) 
{ 
    case 0: 
     if (row == 0) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     CGRect titleFieldFrame = CGRectMake(20.0, 80.0, 280.0, 25.0); 
     UITextField *titleField = [[UITextField alloc] initWithFrame:titleFieldFrame]; 
     [titleField setBorderStyle:UITextBorderStyleRoundedRect]; 
     [titleField setTextColor:[UIColor blackColor]]; 
     [titleField setFont:[UIFont systemFontOfSize:16]]; 
     [titleField setPlaceholder:@"Title"]; 
     [titleField setBackgroundColor:[UIColor whiteColor]]; 
     titleField.keyboardType = UIKeyboardTypeDefault; 
     titleField.returnKeyType = UIReturnKeyDone; 
     [cell addSubview:titleField]; 
    } 
    break;  
return cell; 

}

回答

0

您可以简单地创建一个实例变量并为其指定titleField。或者您可以分配一个标签[titleField setTag:123],然后通过[yourTableView viewWithTag:123]检索它。

+0

它工作。我不知道setTag,但我会研究更多关于它的内容,看看它可以如何使用。感谢DarkDust。 – 2010-08-14 21:30:36

0

给一个特殊的标记到titleField,例如

UITextField *titleField = [[UITextField alloc] initWithFrame:titleFieldFrame]; 
    titleField.tag = 11280492; 

然后用-viewWithTag:来获得该视图。

-(IBAction)saveForm:(id)sender { 
    UITableViewCell* cell = ... 
    UITextField* titleField = [cell viewWithTag:11280492]; 
    NSLog(@"TextField Value => %@", titleField.text); 
} 
+0

嗨KennyTM,我很感谢您的帮助,并且您也帮助我了解了使用Tag的其他方式。 DarkDust的回答看起来简单得多,而且工作。谢谢。 – 2010-08-14 21:35:24