我为UITableView制作了一个自定义的section-header,其中包括一些控件,如分段控件,UIImageView等。它成功出现了,但它不具有可调性,因此我无法与其控件进行交互。
我需要知道如何才能使此标题视图可选?
答案 0 :(得分:23)
使用 UITableViewDelegate无法做到这一点。
你必须在你的头视图中添加一个UITapGestureRecognizer,如:
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
[singleTapRecognizer setDelegate:self];
singleTapRecognizer.numberOfTouchesRequired = 1;
singleTapRecognizer.numberOfTapsRequired = 1;
[yourHeaderView addGestureRecognizer:singleTapRecognizer];
-(void) handleGesture:(UIGestureRecognizer *)gestureRecognizer; //do in this method whatever you want
FOR Swift 3.0
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YOURVIEWCONTROLLER.TapGestureRecognizer(_:)))
yourHeaderView.addGestureRecognizer(tapGesture)
func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
//do your stuff here
}
Swift> = 4.0
添加 UIGestureRecognizerDelegate 继承的类引用
let tap = UITapGestureRecognizer(target: self, action:#selector(self.handleTap(_:)))
tap.delegate = self
self.view.addGestureRecognizer(tap)
@objc func handleTap(_ sender: UITapGestureRecognizer) {
//Do your work
}
可能会有所帮助。
快乐编码。
答案 1 :(得分:5)
这对我有用:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UITableViewHeaderFooterView()
v.textLabel?.text = "Header Text"
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tapRecognizer.delegate = self
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
v.addGestureRecognizer(tapRecognizer)
return v
}
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
print("Tapped")
}
答案 2 :(得分:4)
Swift 3.0的简单解决方案
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UITableViewHeaderFooterView()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
v.addGestureRecognizer(tapRecognizer)
return v
}
func handleTap(gestureRecognizer: UIGestureRecognizer)
{
controller?.view.endEditing(true)
}
答案 3 :(得分:3)
创建自定义部分headerView ..然后添加Tap手势。
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//This method will be called on Tap of section header
- (void)handleTap:(UITapGestureRecognizer *)sender {
//Do the action on Tap
}
答案 4 :(得分:2)
创建用作节头视图的自定义视图并实现该方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
,当你的手指点击它时会调用它。
答案 5 :(得分:2)
您只需在标题视图中添加UIButton(或者标题视图可以是UIButton本身)。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *cellButton = (UIButton*)[cell viewWithTag:1];
[cellButton addTarget:self action:@selector(premiumSectionClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)buttonTapped:(id)sender{
}
答案 6 :(得分:1)
我在Swift中的实现:
在UITableViewController
:
let myView = MyView()
let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
myView.addGestureRecognizer(tapRecognizer)
self.tableView.tableHeaderView = myView
在MyView
:
class MyView: UIView {
func handleTap() {
// Handle touch event here
}
}
答案 7 :(得分:1)
快速解决此问题:
let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
tapR.delegate = self
tapR.numberOfTapsRequired = 1
tapR.numberOfTouchesRequired = 1
yourView.addGestureRecognizer(tapR)
然后实施
func gotoHeaderArticle() {
}
确保让您的调用viewcontroller遵循UIGestureRecognizerDelegate
答案 8 :(得分:1)
如果您需要了解所点击部分的索引,可以使用以下模式( Swift 4 ):
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
// Configure your view here
// ...
let tapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(headerTapped(_:))
)
view.tag = section
view.addGestureRecognizer(tapGestureRecognizer)
return view
}
@objc func headerTapped(_ sender: UITapGestureRecognizer?) {
guard let section = sender?.view?.tag else { return }
// Use your section index here
// ...
}
答案 9 :(得分:0)
Swift 3.1的更新
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TapGestureRecognizer(gestureRecognizer:)))
yourView.addGestureRecognizer(tapGesture)
func TapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
//do your stuff here
print("Gesture")
}
答案 10 :(得分:0)
这对我有用
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
static NSString *cellIdentifier = @"cellIdentifier";
YourHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.imageView.image = [image imageNamed:@"imageName"];
cell.segmentedControl.tag = section;
[cell.segmentedControl addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
return cell;
}
- (void)action:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSLog(@"selected section:%ld",(long)segmentedControl.tag);
}
答案 11 :(得分:0)
如果任何人都需要一个解决方案,该解决方案可以在表视图具有多个部分时起作用,那么我遇到了同样的问题,我想到的解决方案是基于viewForHeaderInSection
方法在按钮上创建不同的目标。部分。我使用的是按钮,但它与UITapGestureRecognizer一样适用
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// ...
if section == 0 {
cell.button.addTarget(self, action: #selector(firstSectionTapped(_:)), for: .touchUpInside)
} else if section == 1 {
cell.button.addTarget(self, action: #selector(secondSectionTapped(_:)), for: .touchUpInside)
}
// ...
}
然后在ViewController中:
@objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section One Tapped")
}
@objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
print("Section Two Tapped")
}