我想在点击 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-推荐答案 strong>
为您的 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/
|