我目前在两个相应的UITableViewCells中有两个UITextField。
它的外观如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.
if ([indexPath section] == 0) {
tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tUser.adjustsFontSizeToFitWidth = YES;
tUser.textColor = [UIColor blackColor];
tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tPass.adjustsFontSizeToFitWidth = YES;
tPass.textColor = [UIColor blackColor];
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
tUser.placeholder = @"@JohnAppleseed";
tUser.keyboardType = UIKeyboardTypeEmailAddress;
tUser.returnKeyType = UIReturnKeyNext;
}
if ([indexPath row] == 1) {
tPass.placeholder = @"Required";
tPass.keyboardType = UIKeyboardTypeDefault;
tPass.returnKeyType = UIReturnKeyDone;
tPass.secureTextEntry = YES;
}
}
tUser.backgroundColor = [UIColor whiteColor];
tUser.autocorrectionType = UITextAutocorrectionTypeNo;
tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
tUser.textAlignment = UITextAlignmentLeft;
tPass.backgroundColor = [UIColor whiteColor];
tPass.autocorrectionType = UITextAutocorrectionTypeNo;
tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
tPass.textAlignment = UITextAlignmentLeft;
tUser.clearButtonMode = UITextFieldViewModeNever;
tPass.clearButtonMode = UITextFieldViewModeNever;
[tUser setEnabled:YES];
[tPass setEnabled:YES];
//[tUser release];
//[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = @"Username";
[cell addSubview:tUser];
[tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_name_preference"]];
}
else {
cell.textLabel.text = @"Password";
[cell addSubview:tPass];
[tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_pass_preference"]];
}
}
return cell; }
正如您所看到的,这些工作以及当我加载UITableView时,UITextFields加载到正确的UITableViewCells中。但是,正如您所看到的,我拉出了两个NSUserDefault对象,它们都放在相应的UITextFields中。
但是,我在导航栏中显示了另一个附加到保存按钮的操作。
-(void)save_clicked: (id) sender {
if ([tPass text] == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"There was no password entered, please enter the correct password and try again."
delegate:self
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
NSLog(@"we can do something here soon...");
//NSString *tUserString = [[NSString alloc] initWithFormat:@"Hello: %@", tUser.text];
NSLog(@"We saved their username: %@", [tUser text]);
NSLog(@"We saved their password: %@", [tPass text]);
// here we will start saving the username and password, then obtaining the authentication shit.
}
}
但是,当我调用这些NSLog时,tUser返回(null)但tPass返回输入的文本。我确信它只是一个简单的语法错误或类似的东西,因为一切(从我的角度来看)看起来应该有效。虽然,但事实并非如此。
有人可以帮助我弄清楚tUser UITextField出了什么问题,以及为什么它会一直返回(null)?
感谢所有帮助!
答案 0 :(得分:0)
每次显示单元格时,您都会添加新的tUser
和tPass
文本字段。您应该在if (cell == nil)
块中保留单元格创建代码(添加文本字段),并在该块之外配置这些文本字段。这样,每次调用tableView:cellForRowAtIndexPath:
时都不会添加新的文本字段。