如何获取UITableView中的所有单元格

比方说,我有一个UITableView有多行。 我想在一定的时间将所有的UITableViewCells作为一个NSArray。 我努力了

[tableView visibleCells] 

但这种方法存在问题:我不能拥有当前不在当前屏幕中的所有单元格。 所以我转向另一种方法:

 -(NSArray *)cellsForTableView:(UITableView *)tableView { NSInteger sections = tableView.numberOfSections; NSMutableArray *cells = [[NSMutableArray alloc] init]; for (int section = 0; section < sections; section++) { NSInteger rows = [tableView numberOfRowsInSection:section]; for (int row = 0; row < rows; row++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // **here, for those cells not in current screen, cell is nil** [cells addObject:cell]; } } return cells; } 

但似乎这种方法仍然不能得到那些没有显示在屏幕上的单元格。 谁可以帮我这个事?

我不认为这是可能的,只是因为tableView不存储所有单元格。 它使用一个caching机制,只存储一些不可见的单元格,以供重用。

你为什么需要所有的细胞? 也许你可以实现,你试图做的,否则。

这是Swift 3.0中最简单的解决scheme

 func getAllCells() -> [UITableViewCell] { var cells = [UITableViewCell]() // assuming tableView is your self.tableView defined somewhere for i in 0...tableView.numberOfSections-1 { for j in 0...tableView.numberOfRowsInSection(i)-1 { if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) { cells.append(cell) } } } return cells } 

每次创build单元格时,都可以将其添加到NSMutableArray,您可以随时parsing它:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *Cell = [self dequeueReusableCellWithIdentifier:@"Custom_Cell_Id"]; if (Cell == NULL) { Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Custom_Cell_Id"]]; [Cells_Array addObject:Cell]; } } - (void) DoSomething { for (int i = 0;i < [Cells count];i++) { CustomCell *Cell = [Cells objectAtIndex:i]; //Access cell components } } 

我认为你可能会误解UITableView的实现。 要理解的重要一点是,不可见的单元格实际上并不存在(或者至less不是)

随着UITableView的滚动,旧的单元格将被replace为新的单元格dequeueReusableCellWithIdentifier

 -(UITableViewCell) cellForRowAtIndexPath (NSIndexPath *)indexPath 

你想要那些文本字段的价值? 如果是,则可以使用indexpath.row通过文本字段的标签属性进行访问

然而,获取当前单元格的一个简单的实现将使用Set即O(1)

 /// Reference to all cells. private var allCells = Set<UITableViewCell>() func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Your_Cell_ID") as! UITableViewCell if !allCells.contains(cell) { allCells.insert(cell) } return cell } 

我没有在我的xcodtesting,但我认为这将帮助你,或者你可以通过这个代码得到想法,并实现自己的。

  -(NSMutableArray *)cellsForTableView:(UITableView *)tableView { NSMutableArray *cells = [[NSMutableArray alloc] init]; //Need to total each section for (int i = 0; i < [tableView numberOfSections]; i++) { NSInteger rows = [tableView numberOfRowsInSection:i]; for (int row = 0; row < rows; row++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; for (UIView *subView in cell.subviews) { if ([subView isKindOfClass:[UITextField class]]){ UITextField *txtField = (UITextField *)subView; [cells addObject:txtField.text]; } } } } return cells; 

}

 -(void) reloadViewHeight { float currentTotal = 0; //Need to total each section for (int i = 0; i < [self.tableView numberOfSections]; i++) { CGRect sectionRect = [self.tableView rectForSection:i]; currentTotal += sectionRect.size.height; } //Set the contentSizeForViewInPopover self.contentSizeForViewInPopover = CGSizeMake(self.tableView.frame.size.width, currentTotal); 

}