Sign in with UITableView

How can I create a login for a username and password, as in the skype application? I know this is a grouped table ... but how can I do this?

I searched the site and found the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UILabel *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)]; if (indexPath.row == 0) startDtLbl.text = @"Username"; else { startDtLbl.text = @"Password"; } startDtLbl.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview:startDtLbl]; UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)]; passwordTF.delegate = self; if (indexPath.row == 0) passwordTF.tag = 0; else { passwordTF.tag = 1; } [cell.contentView addSubview:passwordTF]; } return cell; } 

When I do this:

 NSString * username = [((UITextField*)[self.view viewWithTag:0]) text]; NSString * password = [((UITextField*)[self.view viewWithTag:1]) text]; 

he gives me this error:

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView text]: unrecognized selector sent to instance 0x6c3c600 

Why is this?

+3
source share
2 answers

You also need to install:

 tableView.dataSource = self; 
+1
source

After you designate your view controller as a UITextField delegate, you can access the values ​​of the text fields at different points using the delegate methods below:

 – textFieldShouldBeginEditing: – textFieldDidBeginEditing: – textFieldShouldEndEditing: – textFieldDidEndEditing: 

As an example, in any of these methods you can get the value of textField.text and assign it to NSString, which can be passed to your LOGIN method as needed.

Assuming your login text field has a tag value of 1, and your password text field has a tag value of 2, you can capture the values ​​using the delegation method below:

 - (void) textFieldDidEndEditing:(UITextField *)textField { switch (textField.tag) { case 1: loginString = textField.text; break; case 2: passwordString = textField.text; break; default: break; } } 

You can also capture the links of the corresponding cell and indexPath in any of the above methods using:

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[[textField superview] superview]];

And then into the cell with:

UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];

This would be especially useful if you had a long shape and you want to go to the next field so that the keyboard is not at the top of the cells where you need to enter text. You can also use these links to move the RETURN key on your keyboard to the next UITextField or possibly change the first responder if the user has finished editing the last field of the form.

Hope this makes sense. Hurray, Horn

+3
source

Source: https://habr.com/ru/post/1340584/


All Articles