gpt4 book ai didi

ios - 选择 UITableViewCell 时,UIButton 被覆盖

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:42 25 4
gpt4 key购买 nike

我在我的 UITableViewCell 中使用了一个 UIButton,这个按钮可以被选中,金色星形,未被选中,灰色星形,如下所示。

enter image description here

问题是,当单元格被选中时,无论按钮是否被选中,它都会变成灰色(这种灰色不同于星号未选中模式的灰色)。我一直在努力弄清楚为什么会发生这种情况,但没有成功。

enter image description here

这里是单元格的实现,

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

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]retain];

//set the background of the cells
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"cellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellselected2.png"]];

// set the button content
[button setImage:[UIImage imageNamed:@"star.png" ] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"starSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 10, 24, 24)];

[cell addSubview:button];

return cell;
}

同样,当我在选择单元格时点击按钮时,按钮将完全消失!

谢谢,

最佳答案

我想出了问题所在。 The problem is that when the cell is selected the UIButton in the cell will go to it's "highlighted state".如果没有分配图像用于按钮的按钮突出显示状态,它将看起来与我的情况相同。

所以我只是通过添加我想在 UIButton 突出显示状态下使用的图像来修复它,

[button setImage:[UIImage imageNamed:@"starSelected.png"] forState:UIControlStateHighlighted];

希望这能帮助其他面临同样问题的人:)

关于ios - 选择 UITableViewCell 时,UIButton 被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876894/

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