从自定义单元格iPhone中的textField中获取值 [英] Fetching Values from textField in a custom cell iPhone

查看:103
本文介绍了从自定义单元格iPhone中的textField中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格视图中有自定义单元格,请参见下文。

I have a custom cell in a table View, see below.

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

I have two textFields txt1 and txt2 in my custom cell, as seen below

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

How can i access the values i enter in each of the text fields so that i can add the values in to seperate Arrays...

添加集按钮将增加分组表的部分的数量通过递增另一个集来查看那里。

the "Addset" button will increment the no of sections of the grouped table View there by incrementing the one more set.

我的表视图代码如下。

- (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;
}

全部谢谢..

推荐答案

单元格是可重用的,因此需要更持久的方式来保存其信息。

Cells are reusable, so they need a more persistent way to keep their info.

方法1:
您可以将一些 UITextField 对象保存到如果您不想重复使用文本字段,则在 cellForRowAtIndexPath 中,您只需将文本字段设置为其单元格,例如:

Method 1: You could hold some UITextField objects into an array in case you don't want to reuse the textfields as well, and at cellForRowAtIndexPath you'd only need to set the textfields to their cells such as:

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

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

Method 2: If you want to reuse the textfields as well I suggest using an array with mutable dictionaries, each dictionary holding the 'settings' for a cell. The textfields will be fully managed by the custom cell (e.g: at the UIControlEventValueChanged event update @"txt1" or @"txt2" values from the dictionary attached to the cell).

///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中的textField中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆