我正在尝试为分组的UITableView中的每个单元格创建一个索引计数,这样我就可以将单元格值存储在数组中的正确位置,但是我无法计算出它所需的数学:
索引运行如下,我的计数需要是:
indexPath.section = 0
indexPath.row = 0 cellIndex = 0
indexPath.row = 1 cellIndex = 1
indexPath.row = 2 cellIndex = 2
indexPath.row = 3 cellIndex = 3
indexPath.row = 4 cellIndex = 4
indexPath.section = 1
indexPath.row = 0 cellIndex = 5
indexPath.row = 1 cellIndex = 6
indexPath.row = 2 cellIndex = 7
indexPath.row = 3 cellIndex = 8
到目前为止,这是我的等式,但它无法正常工作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger cellIndex = numberOfSections * indexPath.section + indexPath.row;
}
答案 0 :(得分:1)
我想出了一个实现这个目标的简单方法:
//Number of cells before this
int cellsBefore = 0;
for(int i = indexPath.section - 1; i >= 0; i--) {
cellsBefore += [tableView numberOfRowsInSection:i];
}
//Calculate cellIndex count
NSInteger cellIndex = cellsBefore + indexPath.row;
答案 1 :(得分:0)
我认为您应该将numberOfSections * indexPath.section
更改为numberOfCellsPerSection * indexPath.section
。根据您对答案的评论,您可以将uitextfield值存储到单元格的相应实体中,而不是使用数组。
答案 2 :(得分:0)
您可以使用TLIndexPathTools。 TLIndexPathDataModel
类可以自动将项目组织成各个部分,并且有许多便利API可以简化视图控制器代码。例如,您将获得如下项目的总数:
self.indexPathController.items.count;
有几个示例项目演示了不同的用例。 JSON sample project演示了如何使用sectionNameKeyPath
属性将JSON数据组织到部分中,类似于NSFetchedResultsController
的工作方式(除了项目不需要按照TLIndexPathTools的部分预先排序)。通过创建TLIndexPathSectionInfo
实例,还可以显式创建节,包括空节。请参阅Section Info sample project。