UITableView中的Header接收用户事件

UIKIT_CLASS_AVAILABLE(2_0) @interface  UIControl : UIView {
  @package
    NSMutableArray* _targetActions;
    CGPoint         _previousPoint;
    CFAbsoluteTime  _downTime;
    struct {
        unsigned int disabled:1;
        unsigned int tracking:1;
        unsigned int touchInside:1;
        unsigned int touchDragged:1;
        unsigned int requiresDisplayOnTracking:1;
        unsigned int highlighted:1;
        unsigned int dontHighlightOnTouchDown:1;
        unsigned int delayActions:1;
        unsigned int allowActionsToQueue:1;
        unsigned int pendingUnhighlight:1;
        unsigned int selected:1;
	unsigned int verticalAlignment:2;
	unsigned int horizontalAlignment:2;
    } _controlFlags;
}

由类UIController的定义可以看出,UIController是UIView的子类。但是UIController可以接收用户的Tap事件,所以,在必要的时候可以使用UIController代替UIView来使用。

例如近期正在做的一个App,需要是TableView中每个Section中的Header接收用户的点击事件以触发相应的操作,需要在方法- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中做一些修改才可以处理用户的点击事件。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIControl *result = nil;
    
    result = [[UIControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 34.0f)];
    result.backgroundColor = [UIColor redColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"web-icon.png"]];
    imageView.frame = CGRectMake(2.0f, 2.0f, imageView.frame.size.width, imageView.frame.size.height);
    [result addSubview:imageView];
    
    result.tag = section;
    [result addTarget:self action:@selector(headerIsTapEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    return result;
}

- (void)headerIsTapEvent:(id)sender
{
    NSInteger sectionIndex = [sender tag];
    
    NSLog(@"%i", sectionIndex);
}

这样Section中的Header就可以接收用户的点击事件了。

 

 

你可能感兴趣的:(header,UIView,UITableView,点击事件,UIController)