ios 如何获取 UITableView 中的所有单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13028340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all cells in a UITableView
提问by David L
Let's say I have a UITableView which has multiple rows. I want to get all the UITableViewCells as an NSArray at a certain point of time. I have tried
假设我有一个多行的 UITableView。我想在某个时间点将所有 UITableViewCell 作为 NSArray 获取。我试过了
[tableView visibleCells]
but there is a problem with this approach: I can not have all those cells which are not currently in the current screen. So I turned to another approach:
但是这种方法有一个问题:我不能拥有当前屏幕上没有的所有单元格。所以我转向了另一种方法:
-(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;
}
but seems that this approach still can not get those cells which are not displayed in the screen. Can anyone help me on this?
但似乎这种方法仍然无法获取屏幕中未显示的那些单元格。谁可以帮我这个事?
采纳答案by Tobi
I don't think thats possible, simply because the tableView doesn't store all cells. It uses a caching mechanism which only stores some cells, that are not visible, for reuse.
我不认为那是可能的,仅仅是因为 tableView 不存储所有单元格。它使用一种缓存机制,该机制仅存储一些不可见的单元格以供重用。
Why do you need all cells? Maybe you can achieve, what you're trying to do, otherwise.
为什么你需要所有的细胞?也许您可以实现您正在尝试做的事情,否则。
回答by Vakas
Here is the simplest solution in Swift 3.0
这是 Swift 3.0 中最简单的解决方案
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
}
回答by Unreal Dragon
Each time you create a cell you can add it to a NSMutableArray which you can parse whenever you need:
每次创建单元格时,您都可以将其添加到 NSMutableArray 中,您可以在需要时对其进行解析:
- (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
}
}
回答by eonist
Functional solution swift 5.0
功能解决方案swift 5.0
extension UITableViewDataSource where Self: UITableView {
/**
* Returns all cells in a table
* ## Examples:
* tableView.cells // array of cells in a tableview
*/
public var cells: [UITableViewCell] {
(0..<self.numberOfSections).indices.map { (sectionIndex: Int) -> [UITableViewCell] in
(0..<self.numberOfRows(inSection: sectionIndex)).indices.compactMap { (rowIndex: Int) -> UITableViewCell? in
self.cellForRow(at: IndexPath(row: rowIndex, section: sectionIndex))
}
}.flatMap { self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.contentSize.width, self.tableView.contentSize.height);
}
}
}
回答by Reza Dehnavi
I guess I found a trick for this issue. Try this ;)
我想我找到了解决这个问题的技巧。尝试这个 ;)
-(UITableViewCell) cellForRowAtIndexPath (NSIndexPath *)indexPath
UITableView call cellForRowAtIndexPath
according height of tableView.frame
so you should update the height of your tableView.
UITableViewcellForRowAtIndexPath
根据高度调用,tableView.frame
因此您应该更新 tableView 的高度。
回答by AppleDelegate
I think that you may be misunderstanding how the UITableView
is implemented. The important point to understand is that cells that are not visible don't actually exist (or at least they might not)
我认为您可能误解了如何UITableView
实施。需要理解的重要一点是,不可见的细胞实际上并不存在(或者至少它们可能不存在)
As the UITableView
is scrolled the old cells are replaced by new cells by virtue of dequeueReusableCellWithIdentifier
used in
当UITableView
滚动时,旧单元格被新单元格替换,因为 dequeueReusableCellWithIdentifier
在
/// 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
}
回答by Sudha Chandran B.C.
Do you want value of those text fields? If Yes, you can access via tag property of the textfield with indexpath.row
您想要这些文本字段的值吗?如果是,您可以使用 indexpath.row 通过文本字段的 tag 属性访问
回答by Mazen Kasser
However, a simpler implementation to get the current cells would be using Set i.e O(1)
但是,获取当前单元格的更简单实现是使用 Set 即 O(1)
-(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;
回答by PJR
i have not test in my xcod , but i think this will help you or you can get idea by this code and implement your own .
我还没有在我的 xcod 中进行测试,但我认为这会对您有所帮助,或者您可以通过此代码获得想法并实现您自己的 .
-(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);
}
}
##代码##}
}