根据动态tableview中节标题中控件的操作确定节的索引

时间:2013-10-24 15:44:26

标签: ios objective-c uitableview

我有一个TableViewController,可以随时添加或删除部分和行。每个节头都是一个UITableViewHeaderFooterView,其中添加了一个按钮。

触摸按钮时,如何使用触摸按钮确定部分的索引?

先决条件

有一个驱动tableview的模型对象。该模型有一个属性,返回一个表示节数的数组。数组中的每个对象代表一行。

要求

实施不得依赖于重复的州或模型信息。

将通过tableview动画插入和删除行和部分。 理想情况下,不会调用Tableview reloadData进行更新。

1 个答案:

答案 0 :(得分:2)

定义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对。