如何在UITableView的单元格中循环?

时间:2011-08-02 15:03:40

标签: ios uitableview

我在每个部分都有n个​​部分(已知数量)和X行(未知数量。每行都有一个UITextField。当用户点击“完成”按钮时,我想迭代每个单元格并使用UITextField。如果测试传递来自每个单元格的数据被写入数据库。如果没有,则显示UIAlert。循环遍历行的最佳方法是什么,如果有更优雅的解决方案,请建议。 / p>

8 个答案:

答案 0 :(得分:159)

如果您只想迭代可见单元格,请使用

NSArray *cells = [tableView visibleCells];

如果您想要查看表格的所有单元格,请使用:

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
    for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
    {
        [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
    }
}

现在你可以遍历所有细胞:
CustomTableViewCell 是一个类,其中包含 UITextField 类型的 textField 属性

for (CustomTableViewCell *cell in cells)
{
    UITextField *textField = [cell textField];
    NSLog(@"%@"; [textField text]);
}

答案 1 :(得分:12)

这是一个很适合我的快速实现。

 func animateCells() {
        for cell in tableView.visibleCells() as! [UITableViewCell] {
            //do someting with the cell here.

        }
    }

答案 2 :(得分:5)

对于不了解ObjC的人(像我一样),swift接受了答案。

for section in 0 ..< sectionCount {
    let rowCount = tableView.numberOfRowsInSection(section)
    var list = [TableViewCell]()

    for row in 0 ..< rowCount {
        let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) as! YourCell
        list.append(cell)
    }
}

答案 3 :(得分:3)

for (UIView *view in TableView.subviews) {
    for (tableviewCell *cell in view.subviews) {
       //do
    }
}

答案 4 :(得分:3)

for xcode 9使用它 - (类似于@ 2ank3th但代码更改为swift 4):

let totalSection = tableView.numberOfSections
for section in 0..<totalSection
{
    print("section \(section)")
    let totalRows = tableView.numberOfRows(inSection: section)

    for row in 0..<totalRows
    {
        print("row \(row)")
        let cell = tableView.cellForRow(at: IndexPath(row: row, section: section))
        if let label = cell?.viewWithTag(2) as? UILabel
        {
            label.text = "Section = \(section), Row = \(row)"
        }
    }
}

答案 5 :(得分:2)

由于 iOS可能会回收屏幕外的tableView单元格,因此您必须一次处理一个单元格的表格:

NSIndexPath *indexPath;
CustomTableViewCell *cell;

NSInteger sectionCount = [tableView numberOfSections];
for (NSInteger section = 0; section < sectionCount; section++) {
    NSInteger rowCount = [tableView numberOfRowsInSection:section];
    for (NSInteger row = 0; row < rowCount; row++) {
        indexPath = [NSIndexPath indexPathForRow:row inSection:section];
        cell = [tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"Section %@ row %@: %@", @(section), @(row), cell.textField.text);
    }
}

只有当整个列表可见时,您才能收集所有单元格的NSArray。在这种情况下,使用 [tableView visibleCells] 是安全的。

答案 6 :(得分:1)

快速而肮脏:

for (UIView *view in self.tableView.subviews){
    for (id subview in view.subviews){
        if ([subview isKindOfClass:[UITableViewCell class]]){
            UITableViewCell *cell = subview;
            // do something with your cell
        }
    }
}

答案 7 :(得分:0)

这里有一种完全不同的思考循环UITableView行的方法......这里是一个通过循环遍历数组来更改可能填充UITextView的文本的示例,本质上意味着您的tableView单元格数据。

所有单元格都填充了某种模型的数据。一个非常常见的模型是使用这些对象的NSObject和NSMutableArray。如果你在didSelectRowAtIndexPath中,那么你会想要做这样的事情来影响你在修改上面的数组后选择的行:

for(YourObject *cellRow in yourArray)
{
    if(![cellRow.someString isEqualToString:@""])
    {
         cellRow.someString = @"";
    }
    //...tons of options for conditions related to your data
}

YourObject *obj = [yourArray objectAtIndex:indexPath.row];
obj.someString = @"selected";
[yourArray insertObject:views atIndex:indexPath.row];
[yourArray removeObjectAtIndex:indexPath.row];
[yourTable reloadData];

此代码将删除除您选择的行之外的所有UITextField文本,并保留文本&#34;选择&#34;只要您使用obj.someString在cellForRowAtIndexPath中填充字段的文本或使用 YourObject yourArray yourArray 使用您的阵列来填充单元格的UITextField EM>

这种&#34;循环&#34;不需要任何可见细胞和非可见细胞的条件。如果您有多个由字典数组填充的部分,则可以通过对键值使用条件来使用相同的逻辑。也许你想切换一个单元格imageView,你可以改变代表图像名称的字符串。大量的选项来循环遍历tableView中的数据,而不使用任何委托的UITableView属性。