Objective c 自定义表格单元格iOS 6
在iOS 6中,现在他们重复使用这些单元格,但我不希望这样做,因为它会擦除单元格中的数据。这些单元格都是定制的。它们中有UITextFields,但当我向上滚动时,它会在顶部添加另一个。以下是我注册的方式:Objective c 自定义表格单元格iOS 6,objective-c,xcode,uitableview,ios6,Objective C,Xcode,Uitableview,Ios6,在iOS 6中,现在他们重复使用这些单元格,但我不希望这样做,因为它会擦除单元格中的数据。这些单元格都是定制的。它们中有UITextFields,但当我向上滚动时,它会在顶部添加另一个。以下是我注册的方式:[SettingsTable registerClass:[TextFieldTableCell类]forCellReuseIdentifier:@“textFieldCell”] 以下是-(UITableViewCell*)tableView:(UITableView*)tableView
[SettingsTable registerClass:[TextFieldTableCell类]forCellReuseIdentifier:@“textFieldCell”]代码>
以下是-(UITableViewCell*)tableView:(UITableView*)tableView CellForRowatineXpath:(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,比如textfield“表单元格没有被重用的索引路径”,但是在1视图控制器中,相同的代码只是获取textfields初始文本的不同位置,它将以所有人应该的方式工作
“我也不想要它,因为它会擦除我在单元格中的数据。”
如果发生这种情况,则说明您没有正确执行。尝试以下模式:
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];
请参阅iOS 6的dequeueReusableCellWithIdentifier:
文档:
如果为指定的标识符注册了一个类和一个新单元
必须创建,此方法通过调用其
initWithStyle:reuseIdentifier:method
您已向注册了一个类
[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];
因此,dequeueReusableCellWithIdentifier:
从不返回nil
您应该在TextFieldTableCell
类的initWithStyle:reuseIdentifier:
方法中创建表视图单元格的子视图,例如:
- (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
类中实现prepareforeuse
,以“清理”单元格内容以供重用。它从不为零,我添加==nil,然后分配它,然后告诉它添加完整的单元格textfield,并放入它的文本,它第一次被调用时为零。将代码放在(cell==nil)部分中创建文本字段。我将其记录在其中,if(cell==nil)代码中的代码从未被调用过,多次尝试了多个表。现在的代码确实在if中创建了textfield,每次我转到表时,它都是一个空白调用。只有常规单元格才会记录其nil,但我的自定义单元格不会记录,我还尝试了多种单元格类型,例如带滑块的单元格或带switch@MaxHasADHD-抱歉,这是一个剪切粘贴错误。看看编辑过的代码。我没有复制粘贴它,所以我已经有了这样的代码。我不知道为什么它不是零,下面是确切的代码:下面是textFieldCell每次记录的内容:@MaxHasADHD:这可能意味着你忘了调用[super initWithStyle:…
。查看我的更新答案。它在我的测试项目中起作用,所以我希望它能帮助你!谢谢!我成功了:]我确实忘了它,在另一篇关于如何将uitableviewcell子类化的帖子中发现,我并没有添加这一行,现在它工作得很好!谢谢它记录了两次“没有重复使用表单元格的索引路径”,两次都是在将单元格推离屏幕之后,但第三次它保留了它:[知道解决这个问题的方法吗?@maxhasadd:这似乎是苹果的错误,请参阅。