我在tableview中添加了几个单元格,每个单元格右侧都有一个textField,用户可以输入文本。我还为此添加了一个自定义单元类。我发现当我向下滚动并返回时,前几行的输入将消失。有人知道这是什么问题吗?
这是我的一段代码
@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];
//following condition is not working when i'm scrolling up after writing into some of cell's text fields or i can say never working at all
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]];
}
and 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;
}
检查文本是否输入特定单元格的文本字段的逻辑应该是什么?请帮助!
答案 0 :(得分:0)
嗯 - 你为什么要在每次调用cellForRowAtIndexPath时重新分配一个MyCell?在我看来,你应该喜欢这样的东西:
static NSString *CellIdentifier = @"Cell";
MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
...
}
您使用视图类作为模型类似乎也很奇怪 - 如何维护辅助locations
字典?如果模型实例实际上与视图实例相同,为什么要复制?