gpt4 book ai didi

iOS - UITableViewCell,高亮时设置 subview 背景图片

转载 作者:行者123 更新时间:2023-11-29 12:48:56 33 4
gpt4 key购买 nike

我定义 UITableViewCell(和 subview )如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

cell = [[UITableViewCell alloc]init]; // Without this, subviews stack on one another

... cell stuff (textLabel, etc)

UIButton *buttonDown = [[UIButton alloc] init];
[buttonDown setFrame:CGRectMake(190.0, 7.0, 40.0, 30.0)];
[buttonDown setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
[buttonDown setBackgroundImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateHighlighted];
[buttonDown setTitle:@"-" forState:UIControlStateNormal];
buttonDown.tag = indexPath.row;
[buttonDown addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:buttonDown];

return cell;
}

按钮初始化为正确的背景颜色,当我选择它时按钮正确调用方法,但背景颜色在选择时没有改变。

我试过将它放在方法调用中,如下所示:

- (void) quantityDown:(id)sender {
UIButton *btn = (UIButton *)sender;
[btn setBackgroundImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateHighlighted];
}

但仍然没有骰子。即使是简单的 [btn setBackGroundColor:[UIColor redColor]]; 也无法解决问题。

提前致谢。

编辑#1:

所以我在玩这个,按钮背景图像确实改变了,但前提是你按住按钮(而不是快速点击或点击)。

即使创建自定义单元格也有相同的“必须按住按钮才能更改背景”的要求。自定义单元格定义如下:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];

[self setSelectionStyle:UITableViewCellSelectionStyleNone];

[self.downButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
[self.downButton setBackgroundImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateHighlighted];
}
return self;
}

由于自定义单元格与原始代码具有完全相同的行为,因此我尝试坚持使用原始代码。无论如何,关于为什么延迟/需要按住按钮的任何想法?为什么不立即使用 setBackgroundImage:forState: 方法?

最佳答案

请参阅这篇关于 UIScrollView 中的按钮突出显示延迟的帖子:Is it possible to remove the delay of UIButton's highlighted state inside a UIScrollView?

UITableView 是 UIScrollView 的子类。

关于iOS - UITableViewCell,高亮时设置 subview 背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22898095/

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