2

iOS 6では、セルを再利用するようになりましたが、セル内のデータが消去されるため、私もそれを望んでいません。セルはすべてカスタムです。UITextFieldsが含まれていますが、上にスクロールすると、上に別のUITextFieldsが追加されます。ここにどのように登録するか:[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];

そしてここにコードがあります-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath、

TextFieldTableCell *textFieldCell = (TextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:@"textFieldCell"];
        [textFieldCell textFieldWithLabel];
        textFieldCell.textLabel.text = @"Homepage";
        textFieldCell.textLabel.backgroundColor = [UIColor clearColor];
        textFieldCell.accessoryType = UITableViewCellAccessoryNone;
        [textFieldCell sendSubviewToBack:textFieldCell.textLabel];
        textFieldCell.textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Homepage"];
        textFieldCell.textField.returnKeyType = UIReturnKeyDone;
        [textFieldCell.textField addTarget:self action:@selector(dismissHomepageKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];

これがUITableViewCellサブクラスのコードです。

ヘッダ:

    @interface TextFieldTableCell : UITableViewCell {

    UITextField *textField;

}
@property (nonatomic, retain) UITextField *textField;

-(void)fullTextField;
-(void)textFieldWithLabel;

@end

主要:

`@implementation TextFieldTableCell
@synthesize textField;

-(void)fullTextField {
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 295, 43)];
    self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.contentView addSubview:self.textField];
}

-(void)textFieldWithLabel {
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(123, 0, 177, 43)];
    self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.contentView addSubview:self.textField];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(NSString*)reuseIdentifier {
    return @"textFieldCell";
}

@end`

スクロールするたびにデフォルトにリセットされるので、1つだけを呼び出すように修正するにはどうすればよいですか?また、セル内の何かがnilであるかどうかを確認すると、「再利用されているテーブルセルのインデックスパスがありません」というテキストフィールドがありますが、同じコードでテキストフィールドの初期テキストを取得する場所が異なる場合は、次のように機能します。すべてが必要です

4

2 に答える 2

4

dequeueReusableCellWithIdentifier:iOS6のドキュメントを参照してください。

指定した識別子のクラスを登録し、新しいセルを作成する必要がある場合、このメソッドは、initWithStyle:reuseIdentifier:メソッドを呼び出してセルを初期化します。

にクラスを登録しました

[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];

したがって、をdequeueReusableCellWithIdentifier:返すことはありませんnil

クラスのinitWithStyle:reuseIdentifier:メソッドでテーブルビューセルのサブビューを作成する必要があります。次に例を示します。TextFieldTableCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 295, 43)];
        self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.contentView addSubview:self.textField];
    }
    return self;
}

別の方法は、再利用のためにセルのコンテンツを「クリーンアップ」するためにクラスに実装することprepareForReuseです。TextFieldTableCell

于 2012-10-19T04:54:29.637 に答える
2

「セル内のデータが消去されるので、私もそれを望んでいません。」

これが発生した場合は、正しく実行されていません。このパターンを試してください:

TextFieldTableCell *textFieldCell = (TextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:@"textFieldCell"];
if (textFieldCell == nil) {
  //create your cell
  //not sure how your subclass does this, but you need to alloc and init here
}
//set up your cell data
[textFieldCell textFieldWithLabel]; //What does this actually do? It might cause some of your problems
textFieldCell.textLabel.text = @"Homepage";
textFieldCell.textLabel.backgroundColor = [UIColor clearColor];
textFieldCell.accessoryType = UITableViewCellAccessoryNone;
[textFieldCell sendSubviewToBack:textFieldCell.textLabel];
textFieldCell.textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Homepage"];
textFieldCell.textField.returnKeyType = UIReturnKeyDone;
[textFieldCell.textField addTarget:self action:@selector(dismissHomepageKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
于 2012-10-19T00:49:33.873 に答える