【发布时间】:2012-07-04 23:44:38
【问题描述】:
我已经创建了包含用户名和密码的登录页面。我创建了一个自定义单元格,它有一个标签和一个文本字段。我创建了一个分组表视图并使用了自定义单元格。实际上我已经成功创建,但问题是,如何获取用户名和密码的文本字段值。因为我只为这两个字段使用了一个文本字段。我无法正确获取文本字段值。我总是得到两个字段的最后一个值。如何正确获取用户名和传递 word 文本值?
这是我的示例代码,
在自定义单元格中,
@interface CustomCell : UITableViewCell {
UILabel *userLbl;
UITextField *userTxt;
}
@property (nonatomic, retain) IBOutlet UILabel *userLbl;
@property (nonatomic, retain) IBOutlet UITextField *userTxt;
@end
在根视图控制器中,
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for(id currentObjects in nibObjects)
{
if([currentObjects isKindOfClass:[CustomCell class]])
{
cell = (CustomCell *) currentObjects;
}
}
}
if (indexPath.section == 0) {
if(indexPath.row == 0)
{
[[cell userLbl] setText:@"User Name:"];
cell.userTxt.tag = 0;
//self.userName = [[cell userTxt] text];
}
if (indexPath.row == 1) {
[[cell userLbl] setText:@"Pass Word:"];
cell.userTxt.secureTextEntry = YES;
//self.passWord = [[cell userTxt] text];
cell.userTxt.tag = 1;
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell.
return cell;
}
-(IBAction) submitAction : (id) sender
{
self.userName = [[cell userTxt] text];
self.passWord = [[cell userTxt] text];
[cell.userTxt resignFirstResponder];
NSLog(@"The user Name is %@", self.userName);
NSLog(@"The password is %@", self.passWord);
//Doesn't work
/* UITextField *txtField = (UITextField*)[cell viewWithTag:0];
self.userName = [txtField text];
UITextField *txtField1 = (UITextField*)[cell viewWithTag:1];
self.passWord = [txtField1 text];
NSLog(@"The user Name is %@", self.userName);
NSLog(@"The password is %@", self.passWord);*/
}
这是我的图像屏幕截图,
请帮帮我。
谢谢。
【问题讨论】:
标签: iphone uitableview