UITextFields在UITableView Scroll之后重叠

时间:2012-04-11 19:06:44

标签: objective-c ios uitableview uitextfield

我有一个带UITextFields的UITableView来制作表单。它看起来很棒 - 除了你滚动新的UITextFields放在旧的顶部...我无法弄清楚为什么!我知道当重用单元格时(即dequeueReusableCellWithIdentifier返回非零值),将返回包含现有UITextField的单元格。我找到了一个解决方案,在哪里保持标签的唯一性,最好删除任何以前的UITextField。但我不想这样做。在提交此表单的最后,我想从文本字段中获取所有值(如果我删除它们就不能这样做)..

这是代码。任何帮助表示赞赏!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

cell.accessoryType = UITableViewCellAccessoryNone;

UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(155, 15, 130, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];

if (indexPath.row == 0) {
    playerTextField.placeholder = @"Unique ID Sample";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
}

else if (indexPath.row == 1) {
    playerTextField.placeholder = @"Common Name";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if (indexPath.row == 2) {
    playerTextField.placeholder = @"Scientific Name";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 3) {
    playerTextField.placeholder = @"03/25/1992";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 4) {
    playerTextField.placeholder = @"Male";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 5) {
    playerTextField.placeholder = @"03/01/2012";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 6) {
    playerTextField.placeholder = @"Huntington, WV";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 7) {
    playerTextField.placeholder = @"Huntington, WV";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 8) {
    playerTextField.placeholder = @"Marshall University";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 
else if ([indexPath row] == 9) {
    playerTextField.placeholder = @"Sub Straight";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 10) {
    playerTextField.placeholder = @"Any Light";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 11) {
    playerTextField.placeholder = @"Temperature";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 12) {
    playerTextField.placeholder = @"A Lot";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 13) {
    playerTextField.placeholder = @"All Types";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 14) {
    playerTextField.placeholder = @"Schedule";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 15) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 16) {
    playerTextField.placeholder = @"Ate Someone";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 17) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 18) {
    playerTextField.placeholder = @"The Morgue";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 19) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 20) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 21) {
    playerTextField.placeholder = @"To/ From";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}    

playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;

playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[playerTextField setEnabled: YES];

    [cell addSubview:playerTextField];

[playerTextField release];

cell.textLabel.text = [self.options objectAtIndex:indexPath.row];


return cell;    
}

3 个答案:

答案 0 :(得分:2)

您必须创建自定义UITableViewCell。这样您就可以创建一个ivar,其UITextField可以直接访问。这样,当单元格出列并重复使用时,您可以根据需要进行设置。请务必覆盖单元格子类中的prepareForReuse:并重置添加组件的所有属性。

可能对您的代码有帮助的另一件事是使用typedef enum而不是使用整数来确定它是什么类型的单元格:

typedef enum{
    PlayerFieldId = 0,
    PlayerFieldName,
    PlayerFieldGender
} PlayerFields

然后在您的自定义单元格中,您可以通过上面的枚举设置单元格类型和switch语句:

switch(type){
case PlayerFieldId:
    playerTextField.placeholder = @"Unique ID Sample";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
    break;
    /* other fields here */
}

答案 1 :(得分:0)

您可以创建UITableViewCell的子类,并让它具有您为每个单元格设置的UITextField属性,而不是在您不想删除它们的情况下不断添加新属性。

答案 2 :(得分:0)

使用以下语句创建单元格实例。

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

for (UITextView *txtView in cell.contentView.subviews)
{
    if ([txtView isKindOfClass:[UITextView class]])
    {
        [txtView removeFromSuperview];
    }
}

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}