我有一个UItableViewCell subclass
代码:
#import "RegisterTableViewCell.h"
@implementation RegisterTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_titolo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, self.frame.size.height)];
_titolo.numberOfLines = 0;
_titolo.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview:_titolo];
_linearettangolare = [[UIView alloc]initWithFrame:CGRectMake(100, 10, 1, 25)];
_linearettangolare.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:_linearettangolare];
_input = [[UITextField alloc]initWithFrame:CGRectMake(_linearettangolare.frame.origin.x+6, 10, self.frame.size.width-110, 25)];
[self.contentView addSubview:_input];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
和这样写的cellforrowatindexath
方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
RegisterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RegisterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.titolo.text = @"Conferma password:";
cell.titolo.font = [UIFont boldSystemFontOfSize:13];
if (indexPath.row%2 == 0) {
cell.backgroundColor = [UIColor colorWithRed:143/255.0 green:177/255.0 blue:188/255.0 alpha:1.0/1.0];
cell.titolo.textColor = [UIColor whiteColor];
cell.linearettangolare.backgroundColor = [UIColor whiteColor];
cell.input.textColor = [UIColor whiteColor];
cell.input.secureTextEntry = FALSE;
}
else{
cell.backgroundColor = [UIColor colorWithRed:175/255.0 green:209/255.0 blue:227/255.0 alpha:1.0/1.0];
cell.titolo.textColor = [UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.linearettangolare.backgroundColor = [UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.input.textColor =[UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.input.secureTextEntry = TRUE;
}
return cell;
}
我遇到了以下问题:
当我的UITableView向上和向下滚动时,我将文本添加到inputtextfield后,该内容会“迁移”到另一个UITableViewCell
。
我的代码出了什么问题?
答案 0 :(得分:2)
我认为你的'问题'是细胞被重复使用,它们的内容在重复使用之前永远不会被重置。
在将细胞与prepareForReuse
重复使用之前准备细胞(参见here)
或清除cellForRowAtIndexPath
根据Apple指南,它的首选方式(细胞内容)在cellForRowAtIndexPath
出于性能原因,您应该只重置单元格的属性 与内容无关的内容,例如,alpha,编辑和 选择状态。表视图的委托 tableView:cellForRowAtIndexPath:应始终重置所有内容 重用一个单元格。