Reputation: 1773
I've got an UItableViewCell subclass
with this code:
#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
and a cellforrowatindexath
method written like this:
-(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;
}
I've got the following problem:
When my UITableView scroll up and down, after that I add text to inputtextfield, that content "migrates" to another UITableViewCell
.
What's wrong in my code?
Upvotes: 0
Views: 66
Reputation: 35348
I think your 'problem' is that the cells are reused and their content is never reset before they are reused.
Either prepare the cells before they are reused with prepareForReuse
(see here)
or clear the input field in cellForRowAtIndexPath
According to the Apple guidelines, the preferred way to it (for cell content) is within cellForRowAtIndexPath
For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.
Upvotes: 2