我在UITableView中添加了几个单元格,每个单元格右侧都有一个UITextField,用户可以输入文本。我还为此添加了一个自定义单元类。我发现当我向下滚动并返回时,前几行的输入将消失。有人知道这是什么问题吗?
这是我的一段代码
@implementation MyCell
-(void)setData:(NSString*)str
{
UIImage *image = [UIImage imageNamed:@"cellImage copy.png"];
[[self imageView ]setImage:image];
lblLocations=[[UILabel alloc]init];
lblLocations.textAlignment=UITextAlignmentLeft;
lblLocations.backgroundColor = [UIColor clearColor];
lblLocations.font = [UIFont boldSystemFontOfSize:12];
lblLocations.numberOfLines = 2;
lblLocations.lineBreakMode = UILineBreakModeWordWrap;
lblLocations.textAlignment = UITextAlignmentCenter;
lblLocations.text =str ;
NSLog(@"the title is%@",str);
[[self contentView]addSubview:lblLocations];
[lblLocations release];
txtUnits=[[UITextField alloc]init];
[txtUnits setAdjustsFontSizeToFitWidth:YES];
txtUnits.textAlignment = UITextAlignmentCenter;
txtUnits.returnKeyType = UIReturnKeyDone;
txtUnits.autocapitalizationType = NO;
txtUnits.textColor = [UIColor blackColor];
if ([textDict valueForKey:[NSString stringWithFormat:@"%d",rowNum]]) {
[txtUnits setText:[textDict valueForKey:[NSString stringWithFormat:@"%d",rowNum]]];
}
else
{
[txtUnits setPlaceholder:@"0.00"];
}
[txtUnits setValue:[UIColor blackColor]
forKeyPath:@"_placeholderLabel.textColor"];
[[self contentView]addSubview:txtUnits];
txtUnits.backgroundColor = [UIColor clearColor];
txtUnits.delegate = self;
[txtUnits release];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[textDict setObject:textField.text forKey:[NSString stringWithFormat:@"%d",rowNum]];
}
和cellforrow ....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=nil;
if (cell == nil) {
cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier androw:[indexPath row]] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSMutableDictionary *dict=(NSMutableDictionary*)[locations objectAtIndex:[indexPath section]];
NSMutableArray *array=(NSMutableArray*)[dict objectForKey:@"locationsArray"];
MyCell *modelObj=(MyCell*)[array objectAtIndex:indexPath.row];
NSLog(@"the values of the array are%@",modelObj.title);
NSString *str = modelObj.title;
[(MyCell *)cell setData:str];
return cell;
}