gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:01:16 24 4
gpt4 key购买 nike

我需要将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com