我正在学习 Objective-C 中的 UITableView。你能提示我如何从我的 UIViewController 类访问 UITableviewHeaderFooterView 子类中的 UIButton 吗?以编程方式,因为我不使用 IB。
完整代码:https://gist.github.com/tomnaz/3d790b308d305af8b98c
[[??? btnEdit] addTarget:self
action:@selector(addNewItem:)
forControlEvents:UIControlEventTouchUpInside];
最佳答案
不要在 viewDidLoad 中执行此操作,在 viewForHeaderInSection: 中执行此操作:您有一个指向标题 View 的指针。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";
ItemsHeaderView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
[sectionHeaderView.btnEdit addTarget:self action:@selector(addNewItem:) forControlEvents:UIControlEventTouchUpInside];
return sectionHeaderView;
}
关于ios - 如何访问 UITableViewHeaderFooterView 子类中的 UIButton(或任何 UIElement)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394595/