在 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`
我怎样才能修复它只调用一个,因为每次我滚动它都会将其重置为默认值? 并且如果我检查单元格中的某些内容是否为零,例如文本字段“没有重复使用的表格单元格的索引路径”但是在 1 个 View Controller 中,使用相同的代码只是获取文本字段初始文本的不同位置,它将以这种方式工作都应该
最佳答案
请参阅 iOS 6 的 dequeueReusableCellWithIdentifier:
文档:
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method.
你已经注册了一个类(class)
[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];
因此 dequeueReusableCellWithIdentifier:
永远不会返回 nil
。
您应该在 TextFieldTableCell
类的 initWithStyle:reuseIdentifier:
方法中创建表格 View 单元格的 subview ,例如:
- (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;
}
另一种方法是在您的 TextFieldTableCell
类中实现 prepareForReuse
以“清理”单元格内容以供重用。
关于objective-c - 自定义表格单元格 iOS 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965535/