我使用带有一个部分和两个单元格的 UITableView 创建了一个登录屏幕。这就是这些单元格的创建方式。现在我不知道以后如何从这些单元格中检索值。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"LoginCellIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
leftLabel.backgroundColor = [UIColor clearColor];
leftLabel.tag = 1;
[cell.contentView addSubview:leftLabel];
[leftLabel release];
UITextField *valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 400, 35)];
valueTextField.tag = 2;
valueTextField.delegate = self;
[cell.contentView addSubview:valueTextField];
[valueTextField release];
}
if (indexPath.row == 0) { // User name
UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
lblText.text = @"Username: ";
UITextField *userNameField = (UITextField *)[cell.contentView viewWithTag:2];
userNameField.placeholder = @"Enter your username here";
}
else { // Pass word
UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
lblText.text = @"Password: ";
UITextField *passwordField = (UITextField *)[cell.contentView viewWithTag:2];
passwordField.placeholder = @"Enter your password here";
passwordField.secureTextEntry = YES;
}
return cell;
}
准确地说,我想在用户return按下键时检索值。所以,我想在这里获取单元格的文本字段值......
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%@ is the Value", textField.text);
[textField resignFirstResponder];
return YES;
}
但是,我不知道如何检索这些文本字段的值。我想知道索引路径的 cellForRowAtIndexPath 在这种情况下是否有效?