2013-06-22 66 views
0

我試圖在單擊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

回答

1

一個長鏡頭,但可能是因爲鍵盤被隱藏/顯示或我想象的另一個UI約束取決於單元的數量(嘗試100個單元格可能會導致它在最後一個單元適合屏幕時沒有滾動)和表視圖大小,當您調用resign /成爲第一響應者(即,表視圖顯示不同的單元格)時,可見單元格的數量會發生變化。嘗試首先製作一個陣列副本:

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

+0

感謝@Valentin ..更改代碼後。它的工作..再次感謝 – Sam

4

通過向表格視圖內的文本字段發送becomeFirstResponder消息,可以使其滾動。通過滾動visibleCells數組得到改變。而且,因爲這是在枚舉這個數組的時候發生的,所以你會得到這個異常。

但是,您不需要遍歷可見單元格。只需使用UITableView方法cellForRowAtIndexPath:獲取下一個或上一個單元格。你也不必自己調用resignFirstResponder。當新視圖成爲第一響應者時,舊視圖自動辭職。

相關問題