我正在使用uitaiteView中创建一个表单,当我使用for for(uiview *cell.contentview.subviews 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
对此的任何帮助将不胜感激。
谢谢尼克。
** 编辑 **
这是我最终做的
邓肯C是正确的,抓住这样的信息是不正确的。可以使TableView动画以使可重复使用的单元格中查看并收集数据,但看起来很混乱,这是一种更干净的方法。
-(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];
}
}
您对表观点的工作方式以及应该如何使用它们有概念上的误解。不存在的细胞不存在。表视图仅创建当前可见的细胞。当用户滚动时,从屏幕上滚出的单元格放入回收队列中,然后将新曝光的细胞重新使用。
单元格是视图对象,不应用于保存数据。当用户将数据输入到单元格中(将文本输入文本字段,更改开关或滑块的值等)时,您应该立即收集这些更改并将其保存到表视图显示的模型中。然后,如果单元格在屏幕外滚动并丢弃,您已经收集了其状态数据。