如何在点击按钮上获得UITableviewcell值

时间:2013-05-07 12:29:21

标签: ios uitableview

我想点击Login按钮获取UITableviewCell值。当用户按下登录按钮,然后我想获取第一个单元格(电子邮件)和第二个单元格(密码)值。请帮帮我。

enter image description here

谢谢! SHAILESH

6 个答案:

答案 0 :(得分:3)

请尝试使用这个。我希望它可以帮到你。但是您应该将标签1分配给电子邮件,将2分配给密码文本字段。

-(IBAction)loginBtnPressed:(IBAction)sender
{
   UITableViewCell *emailCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
                                  UITableViewCell *passwordCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] ;//pass
    UITextField *emailTxtFLd = (UITextField *)[emailCell viewWithTag:1];
    UITextField *passwordTxtFLd = (UITextField *)[passwordCell viewWithTag:2];

    NSLog(@"Email : %@",emailTxtFLd.text);
    NSLog(@"Password : %@",passwordTxtFLd.text);
}

答案 1 :(得分:2)

当你将textFiled作为子视图添加到单元格时,你做了一件事,将标签设置为textFiled,就像给定一样,

[textField setTag:-1];

如果您想从textFiled获取文本,请按照以下步骤操作。

// Getting email
CustomCell *tableCell = (CustomCell*)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

UITextField *myTextField = (UITextField *)[tableCell viewWithTag:-1];

NSString *emailString = [myTextField text];
NSLog(@"~~~~ email: %@", emailString);

// Getting Password
tableCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

myTextField = (UITextField *)[tableCell viewWithTag:-1];

NSString *passwordString = [myTextField text];
NSLog(@"~~~~ password: %@", passwordString);

希望这会对你有所帮助:)。

答案 2 :(得分:1)

由于两个单元格都可见,您可以这样询问:

UITableViewCell *emailCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] //email
UITableViewCell *passwordCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] //password

然后假设两个UITableViewCell在UILabel的头文件中都有一个属性,你可以这样做:

emailCell.myLabel.text //Value of the text in the cell

答案 3 :(得分:1)

如果分配了电子邮件和密码,那么您可以在IIBAction方法中直接访问它们

在你的.h文件delcare中这是电子邮件和密码UItextfield

@interface ViewController : UIViewController
{
    UITextField *email;
    UITextField *pass;
}

现在分配并将其添加到您的单元格中

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    email = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
    pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
}



 // Table View Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if(indexPath.row==0)
    {
        [cell addSubview:email];
    }
    if(indexPath.row==1)
    {
        [cell addSubview:pass];
    }

    return cell;
}

现在将此IBAction应用于您的登录按钮

- (IBAction)loginBtnTouchUpInside:(id)sender
{
    NSLog(@"email ==== %@",email.text);
    NSLog(@"pass ==== %@",pass.text);

    NSString *strEmail = email.text; 
    NSString *strPassword = pass.text;
}

我希望这对你有用

答案 4 :(得分:0)

-(void)ActivateLogin{


NSIndexPath *tmpIndexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *tmpIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];

CustomCell *cellCopy0 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath0];
CustomCell *cellCopy1 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath1];


NSString *strEmail = @""; NSString *strPassword = @"";

strEmail = cellCopy0.tf.text;
strPassword = cellCopy1.tf.text;

if(![strEmail isEqualToString:@""] && ![strPassword isEqualToString:@""]){

... //Do what you want to do with the data

}
else{
//Alert user to enter credentials
}

}

答案 5 :(得分:0)

最简单的方法是,将textField值保存在textField委托中的单独字符串中,如下所示。

-(void)textFieldDidEndEditing:(UITextField *)textField{
 stringEmail = textField.text;
}

并使用字符串值进行进一步处理。通过设置标记为它们做。

谢谢。