在UITableView中访问行的内容

时间:2013-07-07 15:57:40

标签: iphone ios uitableview

所以,我正在使用UITableView创建一个登录屏幕。电子邮件和密码有两个文本字段。当用户按下“登录”按钮时,我想将这两个文本字段的内容存储在两个NSStrings中。我怎么做?以下是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;    
}

3 个答案:

答案 0 :(得分:0)

在cellForRowAtIndexPath中,为userid和password字段设置标记,并检索带有标记标识符的文本字段。请查找以下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;
}

答案 1 :(得分: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

}

答案 2 :(得分:0)

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