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

标签 ios objective-c uitableview

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

当一个按钮被触摸时,你如何确定触摸按钮所在部分的索引?

先决条件

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

要求

实现不得依赖于状态或模型信息的重复。

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

最佳答案

定义 UIViews 的实例变量 NSMutableArray(您的部分页脚)。这将作为节页眉/页脚的数据源:

- (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 对。

关于ios - 根据动态 TableView 中节标题中控件的操作确定节的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19570543/

相关文章:

iphone - 以编程方式暂停 mpmovieplayercontroller - 可能吗?

ios - 从 PNMessage 对象的 NSMutableArray 请求消息文本

ios - 如何在 TableView swift 2.0 中对 A - z 进行排序

ios - 带有图像和文本字段的自动布局表格单元格?

ios - 在 Swift 中以编程方式从主 TabBarController 转到嵌套的 ViewController

ios - 如何在 iOS 6 中为文本添加下划线?

ios - Swift 在 TableView 中展开 Optional 值时意外发现 nil

ios - Admob 广告卡住了我的 iOS 游戏 (SpriteKit)

IOS FindObjectsInBackgroundWithBlock -> setNeedsDisplay

ios - 如何计算 UITableViewCell 中的 UILabel 正确行号?