我已經創建了一個使用帶有一個部分和兩個單元格的UITableView的登錄屏幕。這是如何創建這些單元格的。現在我不知道如何從以後的這些單元格中檢索值。使用addSubview動態添加UITextField的UITableViewCell - 當鍵盤上的'返回'命中時如何獲取UITextField值
- (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;
}
準確地說,我希望當用戶點擊回報鍵來檢索值。所以,我想在這裏獲取單元格的文本字段值...
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%@ is the Value", textField.text);
[textField resignFirstResponder];
return YES;
}
但是,我不知道如何檢索這些文本字段的值。我想知道索引路徑的cellForRowAtIndexPath在這種情況下還是不行?
感謝克里斯它適用於一個單元格,但不擊中其他單元格(無論哪個文本字段當前拿着鍵盤)。檢查這個代碼..記得當用戶點擊'返回'鍵時我想獲得價值.. – user653662 2011-03-14 13:43:10
明白了Chris ..我將你的代碼移到了常用函數中,並在textFieldDidEndEditing和textFieldShouldReturn(在做其他事情之前)中調用它。 – user653662 2011-03-14 14:00:23
+1 @ theChrisKent,很好的回答,早點睡覺,謝謝 – NAZIK 2013-01-31 18:30:37