簡體   English   中英

無法從UITableView的文本字段獲取值

[英]Can't get values from text fields from UITableView

我有一個用於輸入用戶名/密碼的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);
    }
..

這僅向我顯示:電子郵件密碼(不標記我輸入的數據)。 如何在這里獲取輸入值?

所有視圖都將其tag默認tag為0。因此, [aCell viewWithTag: 0]返回的第一個標簽為0的視圖-在本例中為單元格的textLabel這也是一個UILabel ,它也響應.text 如果您將cellForRowAtIndexPath:的標記設置為非零值,它將開始為您工作。

在這種情況下,您可以將標簽提供給文本字段,例如
playerTextField.tag = 115
你可以通過標簽來查看
UITextField * aField =(UITextField *)[aCell viewWithTag:115];
並顯示NSLog(@“ text:%@”,aField.text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM