【问题标题】:iPhone- on table view scrolling text fields set to niliPhone-在表格视图滚动文本字段设置为零
【发布时间】:2011-10-28 21:16:59
【问题描述】:

我在表格视图中添加了几个单元格,每个单元格右侧都有一个 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;

}
检查文本是否输入到特定单元格的文本字段中的逻辑应该是什么?请帮忙!

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

    嗯——为什么每次调用 cellForRowAtIndexPath 时都要重新分配一个 MyCell?在我看来,您应该更喜欢以下内容:

    static NSString *CellIdentifier = @"Cell";
    
    MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
    ...
    }
    

    另外,您还使用视图类作为模型类似乎很奇怪——辅助locations 字典是如何维护的?如果实际上模型实例与视图实例相同,为什么要重复?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多