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であるかどうかを確認すると、「再利用されているテーブルセルのインデックスパスがありません」というテキストフィールドがありますが、同じコードでテキストフィールドの初期テキストを取得する場所が異なる場合は、次のように機能します。すべてが必要です