您必须制作自己的自定义标题行,并将其作 for each 部分的第一行.将UITableView
或已经在那里的标头子类化将是一件痛苦的事情.根据他们现在的工作方式,我不确定你能不能轻易地让他们采取行动.您可以设置一个看起来像页眉的单元格,并将tableView:didSelectRowAtIndexPath
设置为手动展开或折叠它所在的部分.
我将存储一个布尔值数组,该数组对应于您的每个部分的"花费"值.然后,您可以让每个自定义标题行上的tableView:didSelectRowAtIndexPath
切换此值,然后重新加载该特定部分.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
///it's the first row of any section so it would be your custom section header
///put in your code to toggle your boolean value here
mybooleans[indexPath.section] = !mybooleans[indexPath.section];
///reload this section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
然后设置numberOfRowsInSection
以判断mybooleans
值,如果该部分未展开,则返回1,或者如果该部分已展开,则返回1+该部分中的项目数.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (mybooleans[section]) {
///we want the number of people plus the header cell
return [self numberOfPeopleInGroup:section] + 1;
} else {
///we just want the header cell
return 1;
}
}
此外,还需要更新cellForRowAtIndexPath
以返回任何节中第一行的自定义标题单元格.