集合<__ NSArrayM:0x837cb90>在枚举时发生了变化

问题描述:

我试图在单击Next按钮时从一个Uitextfield跳转到另一个,它抛出错误信息集合< __NSArrayM:0x837cb90>被枚举时发生了变化。而“上一个”按钮工作正常。集合<__ NSArrayM:0x837cb90>在枚举时发生了变化

当我是桌面视图的倒数第二个单元并想要移动最后一个单元格时,会发生这种情况。 下面是一些代码片段:

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

    NSString *CellIdentifier = @"Cell"; 
    NSUInteger row = [indexPath row]; 

    CustomCell *Cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (Cell == nil) 
    { 

     // a new cell needs to be created 
     Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] ; 
     Cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    NSDictionary* product = [dataArray objectAtIndex:indexPath.row]; 
    Cell.lblProductContName.text=[product objectForKey:@"ProductContentsandName"]; 
    Cell.txtQty.delegate = self; 
    Cell.txtQty.tag = 100 + row; 
    Cell.txtQty.text=[txtFieldArray objectAtIndex:indexPath.row]; 

    return Cell; 

}

代码在Next按钮,上一页和完成按钮。

- (void)onPrev:(id)sender 
{ 
    NSArray *visiableCells = [self.myTableView visibleCells]; 
    [visiableCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     CustomCell *cell = (CustomCell *)obj; 

     if (cell.txtQty.tag == selectedCellIndex) { 
      [cell.txtQty resignFirstResponder]; 
     } else if (cell.txtQty.tag == selectedCellIndex - 1){ 
      [cell.txtQty becomeFirstResponder]; 
      *stop = YES; 
     } 

    }]; 

} 

- (void)onNext:(id)sender 
{ 

    NSArray *visiableCells = [self.myTableView visibleCells]; 


    [visiableCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
      CustomCell *cell = (CustomCell *)obj; 
      if (cell.txtQty.tag == selectedCellIndex) { 
       [cell.txtQty resignFirstResponder]; 
      } else if (cell.txtQty.tag == selectedCellIndex + 1){ 
       [cell.txtQty becomeFirstResponder]; 
       *stop = YES; 
      } 

     }]; 

} 

- (void)onDone:(id)sender 
{ 
    NSArray *visiableCells = [self.myTableView visibleCells]; 

    [visiableCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     CustomCell *cell = (CustomCell *)obj; 
     if (cell.txtQty.tag == selectedCellIndex) { 
      [cell.txtQty resignFirstResponder]; 
     } 


    }]; 

} 
+2

代码格式化?如果你修复它,人们会更倾向于阅读你的问题。 – Abizern

一个长镜头,但可能是因为键盘被隐藏/显示或我想象的另一个UI约束取决于单元的数量(尝试100个单元格可能会导致它在最后一个单元适合屏幕时没有滚动)和表视图大小,当您调用resign /成为第一响应者(即,表视图显示不同的单元格)时,可见单元格的数量会发生变化。尝试首先制作一个阵列副本:

NSArray *visibleCells = [[self.myTableView visibleCells] copy];

+0

感谢@Valentin ..更改代码后。它的工作..再次感谢 – Sam

通过向表格视图内的文本字段发送becomeFirstResponder消息,可以使其滚动。通过滚动visibleCells数组得到改变。而且,因为这是在枚举这个数组的时候发生的,所以你会得到这个异常。

但是,您不需要遍历可见单元格。只需使用UITableView方法cellForRowAtIndexPath:获取下一个或上一个单元格。你也不必自己调用resignFirstResponder。当新视图成为第一响应者时,旧视图自动辞职。