How can I loop through UITableView's cells?

前端 未结 8 1154
时光说笑
时光说笑 2020-12-07 13:04

I have n sections (known amount) and X rows in each section (unknown amount. Each row has a UITextField. When the user taps the \"Done\" button I want to iterate through eac

相关标签:
8条回答
  • 2020-12-07 13:53

    If you only want to iterate through the visible cells, then use

    NSArray *cells = [tableView visibleCells];
    

    If you want all cells of the table view, then use this:

    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]]];
        }
    }
    

    Now you can iterate through all cells:
    (CustomTableViewCell is a class, which contains the property textField of the type UITextField)

    for (CustomTableViewCell *cell in cells)
    {
        UITextField *textField = [cell textField];
        NSLog(@"%@"; [textField text]);
    }
    
    0 讨论(0)
  • 2020-12-07 13:53

    Here is a nice swift implementation that works for me.

     func animateCells() {
            for cell in tableView.visibleCells() as! [UITableViewCell] {
                //do someting with the cell here.
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题