简体   繁体   中英

Can't get values from text fields from UITableView

I have a UITableView that I used for username/password enter. But I have problem to get values when I click on button. This is how I make table:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *kCellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:kCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;

        if ([indexPath section] == 0) {
            UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
            playerTextField.adjustsFontSizeToFitWidth = YES;
            playerTextField.textColor = [UIColor blackColor];
            if ([indexPath row] == 0) {
                playerTextField.placeholder = @"example@gmail.com";
                playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
                playerTextField.returnKeyType = UIReturnKeyNext;
            }
            else {
                playerTextField.placeholder = @"Required";
                playerTextField.keyboardType = UIKeyboardTypeDefault;
                playerTextField.returnKeyType = UIReturnKeyDone;
                playerTextField.secureTextEntry = YES;
            }
            playerTextField.backgroundColor = [UIColor whiteColor];
            playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
            playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            playerTextField.textAlignment = NSTextAlignmentLeft;
            playerTextField.tag = 0;

            playerTextField.clearButtonMode = UITextFieldViewModeNever;
            [playerTextField setEnabled: YES];

            playerTextField.backgroundColor = [UIColor clearColor];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            [cell addSubview:playerTextField];

        }
    }
    if ([indexPath section] == 0) { // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Email";
        }
        else {
            cell.textLabel.text = @"Password";
        }
    }
    return cell;
}

And this is what I have tried in button tap:

...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
        UITextField* aField = (UITextField *)[aCell viewWithTag:0];
        NSLog(aField.text);
    }
..

This prints me just: Email Password (labels not data that I entered). How to get entered values here?

All views default their tag to 0. Because of this, [aCell viewWithTag: 0] is returning the first view with tag zero - in this case the cell's textLabel - which is a UILabel that also responds to .text . If you set the tags in your cellForRowAtIndexPath: to something other than zero it should start working for you.

In that case you can give the tag to textfield like
playerTextField.tag = 115
and you can get view by tag like
UITextField *aField = (UITextField *)[aCell viewWithTag:115];
and display NSLog(@"text:%@",aField.text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM