2

フォームを作成するための UITextFields を持つ UITableView があります。それは素晴らしく見えます-スクロールするときを除いて、新しいUITextFieldが古いものの上に配置されます...理由がわかりません! セルが再利用される (つまり、dequeueReusableCellWithIdentifier が非 nil を返す) 場合、セルは既存の 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;    
}
4

3 に答える 3

2

カスタムを作成する必要がありますUITableViewCell。これにより、直接アクセスできる を使用ivarして を作成できます。UITextFieldこのようにして、セルがデキューされて再利用されるときに、必要なときに簡単に設定できます。セルのサブクラスでオーバーライドprepareForReuse:し、追加したコンポーネントのすべてのプロパティをリセットしてください。

上記のコードで役立つもう 1 つのことは、typedef enum整数を使用する代わりに a を使用して、それがどのタイプのセルであるかを判断することです。

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 */
}
于 2012-04-11T19:22:15.860 に答える
0

UITableViewCell をサブクラス化し、削除したくない場合に新しいセルを常に追加する代わりに、すべてのセルに設定するだけの UITextField プロパティを持たせることができます。

于 2012-04-11T19:16:37.307 に答える
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];
}
于 2014-10-21T07:48:20.857 に答える