[英]Can't get values from text fields from UITableView
我有一个用于输入用户名/密码的UITableView
。 但是当我单击按钮时,我很难获取值。 这是我制作表格的方式:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView 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(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = @"example@gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = @"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = NSTextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
playerTextField.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:playerTextField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = @"Email";
}
else {
cell.textLabel.text = @"Password";
}
}
return cell;
}
这是我在按钮点击中尝试的方法:
...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
UITextField* aField = (UITextField *)[aCell viewWithTag:0];
NSLog(aField.text);
}
..
这仅向我显示:电子邮件密码(不标记我输入的数据)。 如何在这里获取输入值?
所有视图都将其tag
默认tag
为0。因此, [aCell viewWithTag: 0]
返回的第一个标签为0的视图-在本例中为单元格的textLabel
这也是一个UILabel
,它也响应.text
。 如果您将cellForRowAtIndexPath:
的标记设置为非零值,它将开始为您工作。
在这种情况下,您可以将标签提供给文本字段,例如
playerTextField.tag = 115
你可以通过标签来查看
UITextField * aField =(UITextField *)[aCell viewWithTag:115];
并显示NSLog(@“ text:%@”,aField.text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.