1

UITableViewユーザー名/パスワードの入力に使用した があります。しかし、ボタンをクリックすると値を取得するのに問題があります。これは私がテーブルを作る方法です:

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

そして、これは私がボタンタップで試したことです:

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

これにより、次のように出力されます: 電子メール パスワード (入力したデータではなくラベル)。ここで入力された値を取得する方法は?

4

2 に答える 2

4

すべてのビューのデフォルトはtag0 です。このため、[aCell viewWithTag: 0]は、タグがゼロの最初のビュー (この場合はセル) を返します。textLabelこれは、UILabelにも応答する.textです。のタグをcellForRowAtIndexPath:ゼロ以外に設定すると、機能し始めるはずです。

于 2013-09-04T09:37:18.487 に答える
1

その場合、
playerTextField.tag = 115のようにテキストフィールドにタグを付けることができ、 UITextField *aField = (UITextField *)[aCell viewWithTag:115];
のようにタグでビューを取得できます。NSLog(@"text:%@",aField.text);を表示します。

于 2013-09-04T13:46:30.870 に答える