2

我试图从 UITextField 中保存某些值,但是我无法做到这一点,因为每当我滚动时,UITextField 中什么都没有。我正在使用自定义单元格。我知道自定义单元格必须加载到特定视图中,因此我使用 textFieldDidEndEditing 将值存储在数组中。但是,我收到以下错误消息: * 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

非常感谢您的帮助!我的代码在下面,我在 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];
}
4

3 回答 3

0

将 vales 初始化为空字符串

clientS = @"";
productS = @"";
dateS = @"";
amountS = @"";
qtyS = @"";
taxS = @"";
totalS = @"";
于 2012-10-02T03:59:37.470 回答
0

只需使用每个 CellIdentifier 就可以了CellIdentifier,如下所示

并且在这里使用此代码后,不需要创建数组并在其中插入对象,因此不要创建数组只需使用此代码..

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

使用这个 CellIndentifier 插入另一个像“TextCell”和“ButtonCell”

我希望这可以帮助你...

:)

于 2012-10-02T04:37:41.490 回答
0

从您的代码看来,您正试图在表格视图中实现静态单元格。您可能要考虑的是使用情节提要并创建一个仅使用 Interface Builder 支持静态表格单元格的表格视图。这将允许您消除整个cellForRowAtIndexPath:方法。如果您不能使用情节提要,那么您将不得不按照您的方式管理您的单元格,但在我的书中,只要您可以消除代码,就应该这样做。

如果你要使用故事板,你应该知道几件事:

  • 故事板中的视图控制器需要是表视图控制器
  • 您将表格内容字段设置为“静态单元格”
  • 将要使用的文本字段添加到每个单元格中,方法是将它们拖到您添加的单元格中
  • 在视图控制器标题中创建插座,然后在界面构建器中将连接拖到它们

有一些关于使用带有静态单元格的表格视图的教程。它们可能有点长,但是,坚持使用它们,您应该能够产生您正在寻找的结果。

此致。

于 2012-10-02T05:16:18.787 回答