2012-10-02 67 views
2

我想从UITextField中保存某些值,但是我无法这样做,因为每当我滚动时,UITextField中就没有任何内容。我正在使用自定义单元格。我知道自定义单元格必须在特定的视图中加载,因此我使用textFieldDidEndEditing将值存储在数组中。不过,我得到了以下错误消息: *终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: '* - [__ NSArrayM insertObject:atIndex:]:对象不能是零'UITextField在UITableView中滚动时清除 - 自定义单元格

帮助深表感谢! !我的代码如下,我在IOS开发中很新。

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

    if (indexPath.section == 0) { 
    if (indexPath.row == 7) { 
     static NSString *CellIdentifier = @"RevButtonCell"; 

     RevButtonCell *cell = (RevButtonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RevButtonCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
     [cell.revsaveB addTarget:self action:@selector(revaddparse:) forControlEvents:UIControlEventTouchUpInside]; 

     cell.reveditdeleteB.hidden = YES; 
     cell.reveditsaveB.hidden = YES; 
     cell.revsaveB.hidden = NO; 
     cell.revdeleteB.hidden = NO; 

     } 
     return cell; 
    } else { 

     static NSString *CellIdentifier = @"TextCell"; 

     TextCell *cell = (TextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

     cell.detaillabel.text = [revdetailsA objectAtIndex:indexPath.row]; 

     cell.detailtext.placeholder = @"Enter Text"; 
     cell.detailtext.tag = indexPath.row; 
     cell.detailtext.delegate = self; 

     if (indexPath.row == 0) { 
     cell.detailtext.text = dateS; 
     } 
     if (indexPath.row == 1) { 
     cell.detailtext.text = clientS; 
     } 
     if (indexPath.row == 2) { 
     cell.detailtext.text = productS; 
     } 
     if (indexPath.row == 3) { 
     cell.detailtext.text = amountS; 
     } 
     if (indexPath.row == 4) { 
     cell.detailtext.text = qtyS; 
     } 
     if (indexPath.row == 5) { 
     cell.detailtext.text = taxS; 
     } 
     if (indexPath.row == 6) { 
     cell.detailtext.text = totalS; 
     } 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     return cell; 
    } 
    } 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [client insertObject:clientS atIndex:0]; 
    [product insertObject:productS atIndex:0]; 
    [date insertObject:dateS atIndex:0]; 
    [amount insertObject:amountS atIndex:0]; 
    [qty insertObject:qtyS atIndex:0]; 
    [tax insertObject:taxS atIndex:0]; 
    [total insertObject:totalS atIndex:0]; 
} 
+0

看到它不是强制性的,为每个文本字段值创建数组,但是您的每个文本字段值保留在该文本文件中仅使用波纹管代码,但不插入数组中的insertObject。 –

回答

0

初始化的瓦莱斯空字符串作为

clientS = @""; 
productS = @""; 
dateS = @""; 
amountS = @""; 
qtyS = @""; 
taxS = @""; 
totalS = @""; 
+0

@megna我不知道如何将文本保存到这些变量?以及在索引0处存储在数组中的想法? –

+0

我认为你的意思是@“”,而不是“”。一个是NSString文字,另一个是空的C字符串(char *)。 –

+0

@MattLong感谢亚光,这些天'C#'是我的头,在此之前,它是'Java'所以这些愚蠢的错误在我的代码是常见的了。但我有编译器:) –

0

只是利用一切CellIdentifier是不同的使用CellIdentifier像波纹管

,并在这里使用此代码后,不需要创建阵列和插入对象,所以不要创建数组只是使用此代码..

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row]; 

使用CellIndentifier insted的另一种像“TextCell”和“扣式”

我希望这会帮助你...

:)

+0

你应该详细说明你的答案。为每个单元格使用新的单元标识符对于正常的动态内容来说是一个糟糕的主意。实质上它完全消除了细胞再利用。原因表视图可以如此高效,因为您正在重新使用单元格。您使用相同的标识符来确保重新使用实际上正在发生。话虽这么说,在静态细胞的情况下,有被使用的细胞数量有限的,你可以说这是一个简单的访问要直接的细胞的方法。 –

+0

哎@MattLong如果上述不同cellIdentifier是用那么这里不创造储值新阵列所需,是吗? –

+0

是的,这是正确的。我只是建议你在你的答案中解释一下。这是一种合理的方法,但仅仅是因为您没有显示包含大量行的动态内容。 –

0

像你正试图实现静态细胞看起来从您的代码在表视图中。你可能想要考虑的是使用故事板并创建一个支持使用Interface Builder的静态表格单元格的表格视图。这将允许您消除整个cellForRowAtIndexPath:方法。如果你不能使用故事板,那么你将不得不像你一样管理你的单元格,但在我的书中,任何时候你可以消除代码,你应该。

如果你打算使用一个故事板,有几件事情你应该知道:

  • 您的看法情节板中的控制器需要一个表视图控制器
  • 您设置表格内容领域以“静态单元格”
  • 添加你想要将其拖动到细胞中使用到每个电池的文本框您在视图控制器头添加
  • 创造出口,然后拖动连接到他们在Interface Builder

有一些使用静态单元格的表视图的教程。他们可能有点长,然而,坚持他们,你应该能够产生你正在寻找的结果。

此致敬礼。

相关问题