ios - 当单元格不可见时,如何访问和迭代 UITableView 中的 contentView.subviews?

标签 ios objective-c uitableview

我在 UITableView 中创建一个表单,当我使用 for (UIView *view in cell.contentView.subviews){} 收集表格中的所有数据时,即使它遍历了 UITableView 中的每个单元格,它看不到行中尚不可见的任何数据。我如何访问这些数据?

我尝试添加 scrollToRowAtIndexPath 以徒劳地希望它能加载可见行但无济于事。

 for (int i=0; i < countArray.count; i++) { //count needs to be for the cells entire tableView

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0]; //create NSIndexPath for each cell in section 0
        UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath]; //iterating through each cell available in table
        NSLog(@"iterationing through cell \"%i\"",indexPath.row);

         [self.table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        NSLog(@"moving view to \"%i\"",indexPath.row);

        for (UIView *view in  cell.contentView.subviews){ //find views within cell.contentview

            if ([view isKindOfClass:[UITextField class]]){ // narrow those views down to anything with class UITextField

                UITextField* txtField = (UITextField *)view; // instantiate the UITextField

                if (txtField.tag == 201) { //find the tag of the instantiated UITextField
                    if (dataSwitch == 0) { //we are collecting data
                        [tmpArray addObject:[NSString stringWithFormat:@"%@",txtField.text]];
                    }else if (dataSwitch == 1) { //we are distributing data
                        txtField.text = [tmpArray objectAtIndex:i];
                    }
                }
                else if (txtField.tag == 202) { //find the tag of the instantiated UITextField
                    if (dataSwitch == 0) { //we are collecting data
                        [tmpArray addObject:[NSString stringWithFormat:@"%@",txtField.text]];
                    }else if (dataSwitch == 1) { //we are distributing data
                        txtField.text = [tmpArray objectAtIndex:i];
                    }
                }
                else if (txtField.tag == 203) { //find the tag of the instantiated UITextField
                    if (dataSwitch == 0) { //we are collecting data
                        [tmpArray addObject:[NSString stringWithFormat:@"%@",txtField.text]];
                    }else if (dataSwitch == 1) { //we are distributing data
                        txtField.text = [tmpArray objectAtIndex:i];
                    }
                }
                else if (txtField.tag == 204) { //find the tag of the instantiated UITextField
                    if (dataSwitch == 0) { //we are collecting data
                        [tmpArray addObject:[NSString stringWithFormat:@"%@",txtField.text]];
                    }else if (dataSwitch == 1) { //we are distributing data
                        txtField.text = [tmpArray objectAtIndex:i];
                    }
                }
                else if (txtField.tag == 205) { //find the tag of the instantiated UITextField
                    if (dataSwitch == 0) { //we are collecting data
                        [tmpArray addObject:[NSString stringWithFormat:@"%@",txtField.text]];
                    }else if (dataSwitch == 1) { //we are distributing data
                        txtField.text = [tmpArray objectAtIndex:i];
                    }
                }
                else {
                    NSLog(@"Data is being lost %@ with tag %i",[tmpArray objectAtIndex:i], txtField.tag);
                }
            }


            // End of isKindofClass
            if ([view isKindOfClass:[UIImageView class]]){ // narrow those views down to anything with class UIImage
                UIImageView* image = (UIImageView *)view; // instantiate the UIImage

                if (dataSwitch == 0) { //we are collecting data
                    [tmpArray addObject:image];
                    NSLog(@"collecting image data and storing in %i",i);

                }else if (dataSwitch == 1) { //we are distributing data
                    image = [tmpArray objectAtIndex:i];
                }
            }



            if ([view isKindOfClass:[UILabel class]]){ // narrow those views down to anything with class UILabel
                UILabel* label = (UILabel *)view; // instantiate the UILabel

                 if (label.tag == 301) { //find the tag of the instantiated UILabel

                     if (dataSwitch == 0) { //we are collecting data
                        [tmpLabelArray addObject:[NSString stringWithFormat:@"%@",label.text]];
                        NSLog(@"%@",[NSString stringWithFormat:@"%@",label.text]);
                    }
                }
            }



            if ([view isKindOfClass:[UISegmentedControl class]]){ // narrow those views down to anything with class UISegmentedControl
                UISegmentedControl* seg = (UISegmentedControl *)view; // instantiate the UISegmentedControl

                if (dataSwitch == 0) { //we are collecting data
                    [tmpArray addObject:[NSString stringWithFormat:@"%i",seg.selectedSegmentIndex]];


                }
            }
        } // End of Cell Sub View
    }// Counter Loop

如有任何帮助,我们将不胜感激。

谢谢尼克。

**编辑**

这是我最后做的

Duncan C 是对的,像这样获取信息是不正确的。可以为 tableview 设置动画以将可重用的单元格带入 View 并收集数据,但它看起来很乱,这是一种更简洁的方法。

-(void)textFieldDidEndEditing:(UITextField *)textField {

 if(textField.tag >= 0 && textField.tag <= 10) {

      CGPoint location = [self.table convertPoint:textField.frame.origin fromView:textField.superview];
        NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:location]; //get the indexPath of the textField.

      NSString *str1  = [NSString stringWithFormat:@"%i",stack];
      NSString *str2  = [NSString stringWithFormat:@"%i",indexPath.row];
      NSString *str3  = [NSString stringWithFormat:@"%@",textField.text];

      NSMutableArray *cellAnswer = [[NSMutableArray alloc] initWithObjects:str1,str2,str3, nil];

    }
}

最佳答案

您对 TableView 的工作方式以及应该如何使用它们存在概念上的误解。不可见的单元格不存在。 TableView 只创建那些当前可见的单元格。当用户滚动时,滚出屏幕的单元格被放入回收队列,然后重新用作新出现的单元格的新单元格。

单元格是 View 对象,不应该用来保存数据。当用户在单元格中输入数据(在文本字段中键入文本、更改开关或 slider 的值等)时,您应该立即收集这些更改并将它们保存到 TableView 显示的模型中。然后,如果一个单元格滚动到屏幕外并被丢弃,那么您已经收集了它的状态数据。

关于ios - 当单元格不可见时,如何访问和迭代 UITableView 中的 contentView.subviews?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22324789/

相关文章:

ios - 在 UITableView 中禁用滚动(iPhone SDK 3.0)

ios - NSUbiquitousKeyValueStore 不与 iCloud 同步

ios - 如何检测 Scrollview 弹跳 - iOS

ios - 将原始自定义 View 添加到窗口

objective-c - NSOutlineView 仅显示 NSTreeController 层次结构的前 2 层

objective-c - Instagram直接打开UTI

iphone - 如何检测 aurioTouch 项目中最低和最高频率的值

ios - iOS 6 和 iOS 8 之间的 UITableView 渲染差异

ios - 具有 8 个继承单元格类型的 UITableView

swift - 如何在 TableViewCell 中嵌入 AVPlayerViewController