gpt4 book ai didi

ios - 如何遍历 UITableView 的单元格?

转载 作者:IT王子 更新时间:2023-10-29 07:33:29 26 4
gpt4 key购买 nike

我有 n 个部分(已知数量)和每个部分中的 X 行(未知数量。每行都有一个 UITextField。当用户点击“完成”按钮时,我想遍历每个单元格并使用UITextField。如果测试通过,每个单元格的数据都写入数据库。如果没有,则会显示 UIAlert。遍历行的最佳方法是什么,如果有更优雅的解决方案,请提出建议。

最佳答案

如果您只想遍历可见单元格,则使用

NSArray *cells = [tableView visibleCells];

如果你想要表格 View 的所有单元格,那么使用这个:

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

关于ios - 如何遍历 UITableView 的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6914183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com