在iOS 6中,他们现在重用了单元格,但我也不想要它,因为它会删除单元格中的数据。细胞都是定制的。它们中有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`
我怎么能修复只有它被称为一个,因为每次我滚动它重置为默认值? 如果我检查单元格中的某些内容是否为nil,如文本字段“没有表格单元格被重用”,但在1个视图控制器中,使用相同的代码只是获取文本字段初始文本的不同位置,它将按照方式工作一切都应该
答案 0 :(得分:4)
请参阅适用于iOS 6的dequeueReusableCellWithIdentifier:
文档:
如果您为指定的标识符和新单元格注册了一个类 必须创建,此方法通过调用它来初始化单元格 initWithStyle:reuseIdentifier:method。
您已使用
注册了课程[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
以“清理”单元格内容以供重复使用。
答案 1 :(得分: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];