我有一个TableViewController,可以随时添加或删除部分和行。每个部分标题都是一个 UITableViewHeaderFooterView,其中添加了按钮。
触摸按钮时,您将如何确定带有触摸按钮的部分的索引?
先决条件
有一个模型对象驱动表视图。该模型具有一个属性,该属性返回一个表示节数的数组。数组中的每个对象都表示一行。
要求
实现不得依赖于状态或模型信息的重复。
行和节将通过表视图动画插入和删除。理想情况下,不会调用表视图重新加载数据进行更新。
定义一个实例变量NSMutableArray
UIViews
(您的节页脚)。这将充当节页眉/页脚的数据源:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *buttonView = [self.sectionArray objectAtIndex:section];
return buttonView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.sectionArray count];
}
- (void)didPressSectionButton:(id)sender
{
int section = [self.sectionArray indexOfObject:sender];
}
这只是一个基本示例,您可以使用 NSDictionary 为行和节的整个 UITableView 提供支持。使用 KV 对。