0

我试图在单击 Next 按钮时从 UItableview 中的一个 Uitextfield 跳转到另一个,它向我抛出 Collection <__NSArrayM: 0x837cb90> 在枚举时发生突变的错误消息。而上一个按钮工作正常。

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

- (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;

}

下一个按钮、上一个和完成按钮上的代码。

- (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];
        }


    }];

}
4

2 回答 2

4

通过将 becomeFirstResponder 消息发送到表格视图中的文本字段,您可以使其滚动。通过滚动 visibleCells 数组会发生变化。并且由于在您枚举此数组时发生这种情况,您会得到异常。

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

于 2013-06-22T16:32:12.087 回答
1

一个长镜头,但可能是因为键盘被隐藏/显示或另一个 UI 约束,我想这取决于单元格的数量(尝试使用 100 个单元格可能会使其在适合屏幕的最后一个单元格处崩溃而不滚动)和表格视图大小,当您调用 resign/become first responder 时,可见单元格的数量会发生变化(也就是表格视图显示不同的单元格)。尝试先复制数组:

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

于 2013-06-22T16:34:26.937 回答