【问题标题】:Accessing data from table cell从表格单元格访问数据
【发布时间】:2010-08-14 21:07:20
【问题描述】:

我正在创建一个表单,想知道是否有人知道如何从表格单元格中检索 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;

}

【问题讨论】:

    标签: ios iphone uitableview uitextfield cell


    【解决方案1】:

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

    【讨论】:

    • 成功了。我不知道 setTag,但我会对其进行更多研究,看看还能如何使用它。谢谢 DarkDust。
    【解决方案2】:

    给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);
    }
    

    【讨论】:

    • 嘿 KennyTM,感谢您的帮助,您还帮助我了解了使用 Tag 的其他方式。 DarkDust 的答案似乎更简单,也很有效。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2015-08-14
    • 2016-09-01
    • 2011-11-03
    • 1970-01-01
    相关资源
    最近更新 更多