0

所以,我正在使用 UITableView 创建一个登录屏幕。电子邮件和密码有两个文本字段。当用户按下“登录”按钮时,我想将这两个文本字段的内容存储在两个 NSString 中。我怎么做?这是 cellForRowAtIndexPath 的代码:

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

    static NSString *kCellIdentifier = @"Cell";

    UITextField *tf = [[UITextField alloc] init];

    UITableViewCell *cell = [self.tView 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(10, 10, 250, 30)];
            playerTextField.adjustsFontSizeToFitWidth = YES;
            playerTextField.textColor = [UIColor blackColor];
            if ([indexPath row] == 0) {
                playerTextField.placeholder = @"Email";
                playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
                playerTextField.returnKeyType = UIReturnKeyNext;

                tf = jidField = [self makeTextField:@"" placeholder:playerTextField.placeholder];
                [cell addSubview:jidField];

            }
            else {
                playerTextField.placeholder = @"Password";
                playerTextField.keyboardType = UIKeyboardTypeDefault;
                playerTextField.returnKeyType = UIReturnKeyDone;
                playerTextField.secureTextEntry = YES;

            }
            //playerTextField.backgroundColor = [UIColor whiteColor];
            playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
            playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
            playerTextField.textAlignment = NSTextAlignmentLeft;
           // playerTextField.tag = 0;
            //playerTextField.delegate = self;

            playerTextField.clearButtonMode = YES; // no clear 'x' button to the right
            [playerTextField setEnabled: YES];

            [cell addSubview:playerTextField];

        }

    }

    return cell;    
}
4

3 回答 3

0

我还建议您查看免费的 Sensible TableView 框架。似乎非常适合您尝试做的事情,因为该框架会自动从您的字段中获取数据并将它们存储在您希望的任何数据结构中。

于 2013-07-08T01:30:40.103 回答
0

就像 Tarek 说的,这就是你的做法。

在您的 *.h 文件中

@interface v1ViewController : UITableViewController
{

    UITextField IBOutlet *playerEmailTxt;
    UITextField IBOutlet *playerPassword;

}


@property (nonatomic, retain) UITextField IBOutlet *playerEmailTxt;
@property (nonatomic, retain) UITextField IBOutlet *playerPassword;

在您的 *.m 文件中

    ...
    @synthesize playerEmailTxt;
    @synthesize playerPassword;

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

        static NSString *kCellIdentifier = @"Cell";

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

        if (indexPath.section == 1)
        {

          if (indexPath.row == 0)
          {
                playerEmailTxt = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
                playerEmailTxt.adjustsFontSizeToFitWidth = YES;
                playerEmailTxt.textColor = [UIColor blackColor];

                    playerEmailTxt.placeholder = @"Email";
                    playerEmailTxt.keyboardType = UIKeyboardTypeEmailAddress;
                    playerEmailTxt.returnKeyType = UIReturnKeyNext;


                    [cell addSubview:playerEmailTxt];

        }
        else if (indexPath.row == 1)
        {

                playerPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
                playerPassword.adjustsFontSizeToFitWidth = YES;
                playerPassword.textColor = [UIColor blackColor];

                    playerPassword.placeholder = @"Password";
                    playerPassword.keyboardType = UIKeyboardTypeDefault;
                    playerPassword.returnKeyType = UIReturnKeyDone;
                    playerPassword.secureTextEntry = YES;
                   [cell addSubview:playerPassword];

            }

        }

        return cell;    
    }

现在添加您的登录操作

-(IBAction)LoginAction
{
    NSMutableString *usrEmailStr = [NSMutableString stringWithFormat:@"%@", playerEmailTxt];
    NSMutableString *usrPasswdStr = [NSMutableString stringWithFormat:@"%@",  playerPassword];

//Do whatever you want with these strings

}
于 2013-07-07T17:06:14.800 回答
0

在 cellForRowAtIndexPath 中,为用户 ID 和密码字段设置标签,并使用其标签标识符检索文本字段。请找到以下 loginAction 方法的实现。

- (void)loginAction:(id)sender {
    NSIndexPath *userIdRow = [NSIndexPath indexPathForRow:0 inSection:0];
    NSIndexPath *passwordFieldRow = [NSIndexPath indexPathForRow:1 inSection:0];

    UITableViewCell *userIdTableCell = (UITableViewCell *)[tableview   cellForRowAtIndexPath:userIdRow];
    UITableViewCell *passwordFieldTableCell = (UITableViewCell *)[tableview cellForRowAtIndexPath:userIdRow];

    UITextField *userIDField = (UITextField *)[userIdTableCell viewWithTag:1001];
    UITextField *passwordField = (UITextField *)[passwordFieldTableCell viewWithTag:1002];

    // You can store now text to either your instance variables or properties in the following statements
    NSLog(@"%@", userIDField.text];
    NSLog(@"%@", passwordField.text];
}

在 cellForRowAtIndexPath 中,您必须标记您的文本字段

if ([indexPath row] == 0) {
    playerTextField.tag = 1001;
} else {
    playerTextField.tag = 1002;
}
于 2013-07-07T16:36:51.473 回答