开发者

Fetching Values from textField in a custom cell iPhone

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

Fetching Values from textField in a custom cell iPhone

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

Fetching Values from textField in a custom cell iPhone

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 increment开发者_如何转开发ing the one more set.

my table view code is as follows.

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

Thanks all..


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

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

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"];
}


When you make the IBOutlet connections in your UITableViewCell subclass, connect them to properties in the File Owner (the viewController), instead of the view itself. That way you'll be able to access them from your viewController (the UItableViewDataSource)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜