OStack程序员社区-中国程序员成长平台

标题: ios - 通过索引点击 UITableView Header [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:53
标题: ios - 通过索引点击 UITableView Header

我想在点击 UITableView 标题时获取索引。现在我确实将 UIGestureRecognizer 添加到这样的标题中:

- (nullable UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section {

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(sectionTapped];

    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor grayColor];

    [headerView addGestureRecognizer:recognizer];

   // return [self.myTableView headerWithTitle:self.headers[section] totalRows:self.cells.count inSection:section];
    return headerView;
}

-(IBAction)sectionTappedid)sender{

    NSLog(@"tapped header");
}

有没有简单的方法可以在点击时传递Index部分?



Best Answer-推荐答案


为您的 headerview 设置标签,例如 headerView.tag = section;

  - (nullable UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section {

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(sectionTapped];

    UIView *headerView = [UIView new];
    headerView.tag = section;
    headerView.backgroundColor = [UIColor grayColor];

    [headerView addGestureRecognizer:recognizer];

   // return [self.myTableView headerWithTitle:self.headers[section] totalRows:self.cells.count inSection:section];
    return headerView;
   }

 -(IBAction)sectionTappedUITapGestureRecognizer *)recognizer{

    NSLog(@"tapped header==%d",recognizer.view.tag);
    NSLog(@"tapped header == %ld", recognizer.view.tag);
}

Swift 3 及以上版本

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let recognizer = UITapGestureRecognizer(target: self, action: Selector("sectionTapped:"))
let headerView = UIView()
headerView.tag = section
headerView.backgroundColor = UIColor.gray
headerView.addGestureRecognizer(recognizer)
// return [self.myTableView headerWithTitle:self.headers[section] totalRows:self.cells.count inSection:section];
return headerView
}

@IBAction func sectionTapped(_ recognizer: UITapGestureRecognizer) {
print("tapped header==\(recognizer.view?.tag)")
print("tapped header == \(recognizer.view?.tag)")
}

备用请参见 this

关于ios - 通过索引点击 UITableView Header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919530/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4