Iphone 使用UITableView登录

Iphone 使用UITableView登录,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,如何像在skype应用程序中一样为用户名/密码创建登录?我知道这是一个分组表视图。。但是我怎么能做到呢 我搜索了该网站,发现了以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell

如何像在skype应用程序中一样为用户名/密码创建登录?我知道这是一个分组表视图。。但是我怎么能做到呢

我搜索了该网站,发现了以下代码:

 - (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;
}
当我这样做时:

NSString * username = [((UITextField*)[self.view viewWithTag:0]) text];
NSString * password = [((UITextField*)[self.view viewWithTag:1]) text];
它给了我这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView text]: unrecognized selector sent to instance 0x6c3c600
为什么会这样?

您还必须设置:

tableView.dataSource = self;

将视图控制器指定为UITextField委托后,可以使用以下委托方法在不同点访问文本字段的值:

– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
例如,在这些方法中的任何一种方法中,您都可以获取
textField.text
值并将其分配给NSString,该NSString可以根据需要传递给您的登录方法

假设登录文本字段的标记值为1,密码文本字段的标记值为2,则可以使用下面的委托方法获取这些值:

- (void) textFieldDidEndEditing:(UITextField *)textField {
  switch (textField.tag) {
      case 1:
      loginString = textField.text;
      break;
      case 2:
      passwordString = textField.text;
      break;
      default:
      break;
   }
}
您还可以使用以下方法获取上述任何方法中对应单元格和indexPath的引用:

nsindepath*indepath=[self.tableView indexPathForCell:(UITableViewCell*)[[textField superview]superview]]

然后使用以下命令发送到单元格:

UITableViewCell*单元格=(UITableViewCell*)[self.tableView cellforrowatinexpath:indexath]

如果您有一个较长的表单,并且希望滚动到下一个字段,这样键盘就不会位于需要键入的单元格顶部,那么这些表单将特别有用。您还可以使用这些引用使键盘中的返回键移动到下一个UITextField,或者如果用户已编辑完表单的最后一个字段,则可能会退出first responder

我希望这是有道理的。 干杯
Rog

我真蠢,我写了tableView.datasource难怪它总是给我一个错误还有一个问题,我如何获得UITextField?我试过了,但没有成功。请将文本字段放在.h文件中,这样您就可以在.m文件中的任何位置引用它。请点击勾号将我的回复标记为已回答。