user3613119
user3613119

Reputation: 91

Select a UITextField by tag

I have a UITextField inside my UITableViewCell knowing that my UITableView contains 3 rows, and I'm differentiating each UITextField putting a new tag like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
    cell.myTextField.tag = indexPath.row;
...
}

It is known also that there is a function called 'getByTag', which is so called must catch the existing text in the UITextField whose tag equals 2, so I tried this:

-(void)getByTag{

NSLog(@"Text is -> %@",cell.myTextField.tag[2].text);

}

The code myTextField.tag[2] did not work as I expected because I believe this means selecting not work very well and give me a compiler error. Could someone give me some help on how I can get around this error compilation and make my program select the existing text in my UITextField whose tag is equal to 2?

Upvotes: 0

Views: 99

Answers (1)

Nate Lee
Nate Lee

Reputation: 2842

Try this:

-(void)getByTag:(NSInteger)myTag {
    NSMutableArray *cells = [[NSMutableArray alloc] init];
    for (NSInteger j = 0; j < [self.tableView numberOfSections]; ++j)
    {
        for (NSInteger i = 0; i < [self.tableView numberOfRowsInSection:j]; ++i)
        {
            [cells addObject:[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
        }
    }

    for (MyCustomTableViewCell *cell in cells)
    {
        if (cell.myTextView.tag == tag) {
            NSLog(@"Text is -> %@", cell.myTextField.text);
        }
    }
}

And call it by:

[self getByTag:2];

Upvotes: 2

Related Questions