如何在 UITableviewCell 中单击时更改 UIButton 的背景颜色?
How can i change background color of UIButton on click in UITableviewCell?
我有 UITableviewCell,我在单元格中放置了 4 个按钮。当我点击一个按钮时,我需要将其背景颜色更改为红色。
现在我已经为此编写了代码,当我单击一个按钮时,该按钮的背景颜色没有改变,而不是其他行中的同一个按钮改变了背景颜色。
用例:
1.I UITableView 中有 10 行,每个单元格包含 4 个按钮,名称为
"Good","Better","Best","Worst".
- 当我点击第一行的 "Good" 按钮时,我希望它的颜色会变为红色。
3.Right 现在,如果我单击第一行的 "Good " 按钮,那么在向下滚动时它不会改变颜色,但我可以看到第 4 和第 8 行的 "Good" 按钮变为红色.
所以我的代码在更改颜色时出现了一些错误。
请检查我的表格视图代码和按钮点击代码
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int sectionCount;
if(section==0)
{
sectionCount=4;
}else if(section==1){
sectionCount=4;
}else if (section==2){
sectionCount=3;
}else if(section==3){
sectionCount=1;
}
return sectionCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sharedManager=[Mymanager sharedManager];
static NSString *CellIdentifier = @"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[questioncell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.excellentButton.tag=indexPath.row;
cell.goodButotn.tag=indexPath.row;
cell.fineButton.tag=indexPath.row;
cell.dorrButton.tag=indexPath.row;
if(indexPath.section==0){
cell.question.text=[questions objectAtIndex:indexPath.row];
} else if(indexPath.section==1){
cell.question.text=[section1 objectAtIndex:indexPath.row];
}else if(indexPath.section==2){
cell.question.text=[section2 objectAtIndex:indexPath.row];
}else if(indexPath.section==3){
cell.question.text=[section3 objectAtIndex:indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
按钮点击代码
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
NSInteger row = button.tag; // recover the row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Good" forKey:key];
cell.goodButotn.layer.backgroundColor=[[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
cell.betterButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.bestButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.worstButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
}
请帮我解决这个问题
您不需要使用按钮层,设置背景颜色就足够了(我假设您可以通过 cell.Button 访问您的按钮)
cell.goodButton.backgroundColor = [[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
...
cell.betterButton.backgroundColor = [UIColor clearColor];
cell. bestButton.backgroundColor = [UIColor clearColor];
cell. worstButton.backgroundColor = [UIColor clearColor];
UIButton *button = (UIButton *)sender;
<-- 这已经是您对该按钮的引用。我看到有 2 种方法可以做到这一点
创建自定义 UITableViewCell
并实施重置所有颜色和设置正确颜色的方法。这是干净的方式。在这里你可以实现更多的逻辑并始终拥有正确的对应数据。这里你的每个按钮调用一个自己的方法,你也有一个清晰的方法。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] )
{
[excellentButton addTarget:self action:@selector(clickedExcellentButton) forControlEvents:UIControlEventTouchDown]; // or TouchUp, how ever you like
[goodButton addTarget:self action:@selector(clickedGoodButton) forControlEvents:UIControlEventTouchDown];
[fineButton addTarget:self action:@selector(clickedFineButton) forControlEvents:UIControlEventTouchDown];
[dorrButton addTarget:self action:@selector(clickedWoButton) forControlEvents:UIControlEventTouchDown];
}
return self;
}
-(UIColor *)selectionColor
{
return [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
}
-(void)resetSelection
{
excellentButton.backgroundColor = [UIColor clearColor];
goodButton.backgroundColor = [UIColor clearColor];
fineButton.backgroundColor = [UIColor clearColor];
dorrButton.backgroundColor = [UIColor clearColor];
}
-(void)clickedExcellentButton
{
[self resetSelection];
excellentButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Excellent" forKey:key]; // if you have your sharedManager object here. If you cannot access it from here, you have to forward it or give the cell a reference to it
}
-(void)clickedGoodButton
{
[self resetSelection];
goodButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Good" forKey:key];
}
...
或
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // this is the button that was clicked
// [...]
for(UIButton *ctrl in [button.superview subviews]) // button.superview get the view that holds him. Subviews all the others in his layer
{
if([ctrl isKindOfClass:[UIButton class]])
{
ctrl.backgroundColor = [UIColor clearColor];
}
}
button.backgroundColor = [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
我创建了一个自定义的 tableview 单元格,这四个按钮在按下按钮时使用相同的 IBAction。然后更改该按钮的背景颜色。有效。
表格视图
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil][0];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
按钮点击代码
- (IBAction)actionButtonPressed:(UIButton *)sender {
[sender setBackgroundColor:[UIColor redColor]];
}
我有 UITableviewCell,我在单元格中放置了 4 个按钮。当我点击一个按钮时,我需要将其背景颜色更改为红色。
现在我已经为此编写了代码,当我单击一个按钮时,该按钮的背景颜色没有改变,而不是其他行中的同一个按钮改变了背景颜色。
用例: 1.I UITableView 中有 10 行,每个单元格包含 4 个按钮,名称为 "Good","Better","Best","Worst".
- 当我点击第一行的 "Good" 按钮时,我希望它的颜色会变为红色。
3.Right 现在,如果我单击第一行的 "Good " 按钮,那么在向下滚动时它不会改变颜色,但我可以看到第 4 和第 8 行的 "Good" 按钮变为红色.
所以我的代码在更改颜色时出现了一些错误。
请检查我的表格视图代码和按钮点击代码
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int sectionCount;
if(section==0)
{
sectionCount=4;
}else if(section==1){
sectionCount=4;
}else if (section==2){
sectionCount=3;
}else if(section==3){
sectionCount=1;
}
return sectionCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sharedManager=[Mymanager sharedManager];
static NSString *CellIdentifier = @"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[questioncell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.excellentButton.tag=indexPath.row;
cell.goodButotn.tag=indexPath.row;
cell.fineButton.tag=indexPath.row;
cell.dorrButton.tag=indexPath.row;
if(indexPath.section==0){
cell.question.text=[questions objectAtIndex:indexPath.row];
} else if(indexPath.section==1){
cell.question.text=[section1 objectAtIndex:indexPath.row];
}else if(indexPath.section==2){
cell.question.text=[section2 objectAtIndex:indexPath.row];
}else if(indexPath.section==3){
cell.question.text=[section3 objectAtIndex:indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
按钮点击代码
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
NSInteger row = button.tag; // recover the row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Good" forKey:key];
cell.goodButotn.layer.backgroundColor=[[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
cell.betterButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.bestButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.worstButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
}
请帮我解决这个问题
您不需要使用按钮层,设置背景颜色就足够了(我假设您可以通过 cell.Button 访问您的按钮)
cell.goodButton.backgroundColor = [[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
...
cell.betterButton.backgroundColor = [UIColor clearColor];
cell. bestButton.backgroundColor = [UIColor clearColor];
cell. worstButton.backgroundColor = [UIColor clearColor];
UIButton *button = (UIButton *)sender;
<-- 这已经是您对该按钮的引用。我看到有 2 种方法可以做到这一点
创建自定义 UITableViewCell
并实施重置所有颜色和设置正确颜色的方法。这是干净的方式。在这里你可以实现更多的逻辑并始终拥有正确的对应数据。这里你的每个按钮调用一个自己的方法,你也有一个清晰的方法。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] )
{
[excellentButton addTarget:self action:@selector(clickedExcellentButton) forControlEvents:UIControlEventTouchDown]; // or TouchUp, how ever you like
[goodButton addTarget:self action:@selector(clickedGoodButton) forControlEvents:UIControlEventTouchDown];
[fineButton addTarget:self action:@selector(clickedFineButton) forControlEvents:UIControlEventTouchDown];
[dorrButton addTarget:self action:@selector(clickedWoButton) forControlEvents:UIControlEventTouchDown];
}
return self;
}
-(UIColor *)selectionColor
{
return [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
}
-(void)resetSelection
{
excellentButton.backgroundColor = [UIColor clearColor];
goodButton.backgroundColor = [UIColor clearColor];
fineButton.backgroundColor = [UIColor clearColor];
dorrButton.backgroundColor = [UIColor clearColor];
}
-(void)clickedExcellentButton
{
[self resetSelection];
excellentButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Excellent" forKey:key]; // if you have your sharedManager object here. If you cannot access it from here, you have to forward it or give the cell a reference to it
}
-(void)clickedGoodButton
{
[self resetSelection];
goodButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:@"Good" forKey:key];
}
...
或
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // this is the button that was clicked
// [...]
for(UIButton *ctrl in [button.superview subviews]) // button.superview get the view that holds him. Subviews all the others in his layer
{
if([ctrl isKindOfClass:[UIButton class]])
{
ctrl.backgroundColor = [UIColor clearColor];
}
}
button.backgroundColor = [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
我创建了一个自定义的 tableview 单元格,这四个按钮在按下按钮时使用相同的 IBAction。然后更改该按钮的背景颜色。有效。
表格视图
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil][0];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
按钮点击代码
- (IBAction)actionButtonPressed:(UIButton *)sender {
[sender setBackgroundColor:[UIColor redColor]];
}