Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UiTableViewHeader-动态按钮事件处理程序_Ios_Objective C_Uitableview - Fatal编程技术网

Ios UiTableViewHeader-动态按钮事件处理程序

Ios UiTableViewHeader-动态按钮事件处理程序,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我需要将uiButton添加到静态uitableview节标题-我尝试了以下操作- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // you can get the title you statically defined in the storyboard NSString *sectionTitle = [self tableView:ta

我需要将uiButton添加到静态uitableview节标题-我尝试了以下操作-

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // you can get the title you statically defined in the storyboard
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    CGRect frame = tableView.bounds;
    // create and return a custom view
    #define LABEL_PADDING 10.0f
    HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;

    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];

    addButton.backgroundColor = [UIColor whiteColor];
    addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
    addButton.titleLabel.tintColor = [UIColor blackColor];


    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    title.text = @"iawn";

    customLabel.text = sectionTitle;

    customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
    customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
    [customLabel addSubview:addButton];
    [addButton addSubview:title];
     [addButton addTarget:self action:@selector(receiverButtonClicked:)  forControlEvents:UIControlEventTouchDown];
return customLabel;
}

-(void)receiverButtonClicked:(id)sender{
    NSLog(@"button clicked");
}

上面添加了一个按钮-但对点击事件没有反应-有人能建议我如何让它工作吗

默认情况下,UILabel不处理触摸

添加以下代码行:

customLabel.userInteractionsEnabled = YES;
为了仅显示第三部分的按钮,应添加以下条件:

if(section == 2){...}
因此,您的
-tableView:viewForHeaderInSection:
应如下所示:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section {
    if(section != 2) return nil;
    // you can get the title you statically defined in the storyboard
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    CGRect frame = tableView.bounds;
    // create and return a custom view
    #define LABEL_PADDING 10.0f
    HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;

    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];

    addButton.backgroundColor = [UIColor whiteColor];
    addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
    addButton.titleLabel.tintColor = [UIColor blackColor];


    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    title.text = @"iawn";

    customLabel.text = sectionTitle;

    customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
    customLabel.userInteractionsEnabled = YES;
    customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
    [customLabel addSubview:addButton];
    [addButton addSubview:title];
     [addButton addTarget:self action:@selector(receiverButtonClicked:)  forControlEvents:UIControlEventTouchDown];
return customLabel;
}

不客气。你可以查看我的更新以获得完整答案。