2011-08-03 39 views
1

我正在使用UITableView来显示我的应用程序首选项。
我得到的细胞与此:UITableViewCell中的UiTextField在滚动时变空

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSInteger section = [indexPath section]; 
    NSInteger row = [indexPath row]; 

    NSInteger identifier = row + ((section + 1) * 10); 

    NSString *CellIdentifier = [NSString stringWithFormat:@"CustomCellIdentifier-%d",identifier]; 

    LoginTableViewCell *cell = (LoginTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableViewCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 

     cell.field.tag = identifier; 

     if (section == 0) { 

      switch (row) { 
       case 0:{ 
        NSString *user = [[NSUserDefaults standardUserDefaults] valueForKey:@"username"]; 
        cell.field.text = user; 
        cell.label.text = @"User"; 
        break; 
       } 
       case 1:{ 
        NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier]; 
        NSError *error = nil; 
        NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"]; 
        NSString *pwd = [SFHFKeychainUtils getPasswordForUsername:user andServiceName:bundleName error:&error]; 
        cell.field.text = pwd; 
        cell.field.secureTextEntry = YES; 
        cell.label.text = @"Password"; 
        break; 
       } 
       case 2:{ 
        NSString *numero = [[NSUserDefaults standardUserDefaults] valueForKey:@"numero"]; 
        cell.field.text = numero; 
        cell.field.keyboardType = UIKeyboardTypePhonePad; 
        cell.label.text = @"Number"; 
        break; 
       } 
       default: 
        break; 
      } 

      if (cell.field.text > 0) { 
       cell.field.enabled = NO; 
       cell.field.borderStyle = UITextBorderStyleNone; 
      } 

     } else { 
      switch (row) { 
       case 0:{ 
        cell.field.text = [NSString stringWithFormat:@"+%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"prefix"]]; 
        cell.field.keyboardType = UIKeyboardTypePhonePad; 
        cell.label.text = @"Prefix"; 
        break; 
       } 

       case 1:{ 
        cell.field.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"sender"]; 
        cell.field.keyboardType = UIKeyboardTypeNamePhonePad; 
        cell.label.text = @"Sender"; 
        break; 
       } 
       default: 
        break; 
      } 

     } 

    } 

    return cell; 
} 

的问题是,当一排滚动窗口外,文本字段得到空。 我该如何预防?
我认为缓存是正确的解决方案,但即使有可重用的标识符,我仍然有同样的问题。

回答

0

您正在使用的方法,即从nib文件加载表格单元格,要求 - 为了拥有正确的可重复使用的单元格 - 要在nib“标识符”字段中分配相同的字符串标识符码。也就是说,如果用于缓存双端队列的单元标识符是“MyCellId”,那么nib文件中的UITableCellView“标识符”字段必须包含相同的“MyCellId”标识符。 但是随着你接下来的方法,这是不可能的,因为单元ID是动态生成的,所以当然不能动态地将单元标识符分配给装入了nib的单元。 理论上,您必须为您生成的每个MyCellId-%d标识符创建一个特定的nib文件。 正如我假设您只定义了一个表格单元格,请删除动态标识符生成并使用静态名称,然后将此名称分配给单元格nib文件中的“标识符”字段。

+0

你说得对。看起来问题与笔尖文件标识符有关。这就是我讨厌IB的原因。我会以编程方式做旧式的。谢谢! – pasine

相关问题