ios - 通过索引点击 UITableView Header
<p><p>我想在点击 <code>UITableView</code> 标题时获取索引。现在我确实将 <code>UIGestureRecognizer</code> 添加到这样的标题中:</p>
<pre><code>- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UITapGestureRecognizer *recognizer = [ initWithTarget:self action:@selector(sectionTapped:)];
UIView *headerView = ;
headerView.backgroundColor = ;
;
// return totalRows:self.cells.count inSection:section];
return headerView;
}
-(IBAction)sectionTapped:(id)sender{
NSLog(@"tapped header");
}
</code></pre>
<p>有没有简单的方法可以在点击时传递<code>Index</code>部分?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>为您的 <code>headerview</code> 设置标签,例如 <code>headerView.tag = section;</code></p>
<pre><code>- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UITapGestureRecognizer *recognizer = [ initWithTarget:self action:@selector(sectionTapped:)];
UIView *headerView = ;
headerView.tag = section;
headerView.backgroundColor = ;
;
// return totalRows:self.cells.count inSection:section];
return headerView;
}
-(IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer{
NSLog(@"tapped header==%d",recognizer.view.tag);
NSLog(@"tapped header == %ld", recognizer.view.tag);
}
</code></pre>
<p><strong>Swift 3 及以上版本</strong></p>
<pre><code>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 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)")
}
</code></pre>
<p>备用请参见 <a href="https://stackoverflow.com/questions/10861669/add-a-gesture-recognizer-to-tableview-header-doesnt-work" rel="noreferrer noopener nofollow">this</a> </p></p>
<p style="font-size: 20px;">关于ios - 通过索引点击 UITableView Header,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38919530/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38919530/
</a>
</p>
页:
[1]