ios - UiTableView Header - 动态按钮事件处理程序

标签 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: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;
}

关于ios - UiTableView Header - 动态按钮事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019216/

相关文章:

ios - 'UITableViewCell' 没有可见的@interface 声明选择器 'dequeueReusableCellWithIdentifier:'

ios - 如何使用 NSPredicate 在 NSDate 中按天分组 - 需要创建多个 UITableView 部分

iphone - iOS:如何创建和绘制(并保存)比屏幕大的图像?

objective-c - 无法在我的 iPad 模拟器中显示正确的 URL

ios - 从外部源设置 UITableViewCell 内部的内容?

android - iOS 和 Android 的 OpenGL ES 区别

java - key key = new SecretKeySpec(keyValue, "AES");在 Objective C 中

iphone - 如何使推送通知打开特定的uiview

ios - View 的 hidden = yes 和 alpha = 0.0f 有什么区别

ios - iPhone 应用程序无法在 iPad 上以 2 倍分辨率运行