作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有 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/
我是一名优秀的程序员,十分优秀!