iphone - 从自定义单元格 iPhone 中的 textField 获取值

标签 iphone ios uitextfield

我在 TableView 中有一个自定义单元格,请参见下文。

enter image description here

我的自定义单元格中有两个文本字段 txt1 和 txt2,如下所示

enter image description here

我如何访问我在每个文本字段中输入的值,以便我可以将这些值添加到单独的数组中...

“添加集”按钮将通过增加一组来增加分组 TableView 的部分数。

我的表格 View 代码如下。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *cellID= @"catogoryCell";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellID];

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

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass: [CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
            }
        }
    }

    //cell.txt1.text = @"0"; 

    return cell;
}

谢谢大家..

最佳答案

单元格是可重复使用的,因此它们需要一种更持久的方式来保存它们的信息。

方法一: 您可以将一些 UITextField 对象保存到一个数组中,以防您也不想重复使用文本字段,并且在 cellForRowAtIndexPath 中,您只需要将文本字段设置为他们的细胞如:

cell.txt1 = [textFieldsArray objectAtindex:indexPath.section*2];
cell.txt2 = [textFieldsArray objectAtindex:indexPath.section*2+1]; //txt1 and txt2 properties should have assign

方法二: 如果你也想重用文本字段,我建议使用一个带有可变字典的数组,每个字典保存一个单元格的“设置”。文本字段将完全由自定义单元格管理(例如:在 UIControlEventValueChanged 事件中更新附加到单元格的字典中的 @"txt1"或 @"txt2"值)。

///somewhere in the initialization (in the class holding the tableview)
contentArray = [[NSMutableArray alloc] init];

///when adding a new cell (e.g: inside the 'Add set' action)
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"", @"txt1", @"", @"txt2", nil]];
//add a new cell to the table (the same way you do now when tapping 'Add set')

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
     [cell attachDictionary:[contentArray objectAtIndex:indexPath.section]];
    return cell;
}

///anywhere where you'd like to access the values inserted inside a cell
NSMutableDictionary *cell3Content = [contentArray objectAtIndex:3];
NSString *text1 = [cell3Content valueForKey:@"txt1"];
NSString *text2 = [cell3Content valueForKey:@"txt2"];

///CustomCell.m
-(id)initWithCoder:(NSCoder *)decoder{
    self = [super initWithCoder:decoder];
    if(!self) return nil;
    [txt1 addTarget:self action:@selector(txt1Changed:) forControlEvents:UIControlEventValueChanged];
    [txt2 addTarget:self action:@selector(txt2Changed:) forControlEvents:UIControlEventValueChanged];
    return self;
}
-(void)attachDictionary:(NSMutableDictionary *)dic{
    contentDictionary = dic;
    txt1.text = [contentDictionary valueForKey:@"txt1"];
    txt2.text = [contentDictionary valueForKey:@"txt2"];
}
-(void)txt1Changed:(UITextField *)sender{
    [contentDictionary setValue:txt1.text forKey:@"txt1"];
}

关于iphone - 从自定义单元格 iPhone 中的 textField 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7344247/

相关文章:

ios - 通过 URL Scheme 或其他方式打开 iOS 11 文件应用

ios - UITextField 在执行字符限制的同时附加/在日期之间

iphone - 限制 NSTableView 列中单元格中的文本长度

iphone - 无法在 NSManagedObject 类 'ClassName' 上调用指定的初始值设定项

iphone - 在 iPhone 的应用程序中访问本地短信数据

iphone - 如何检测 iPhone 上的环境声级?

ios - 如何在其他 UIViewController 中显示 UIViewController?

iphone - 如何使 Web URL 在从应用程序打开时不显示动画?

ios - 如何将用户输入从 6 个 OTP UITextFields 存储到属性,然后将值传递给 Firebase 电话身份验证以在 Swift 4 中进行验证

objective-c - 如何从文本字段获取数值?