【发布时间】:2015-03-25 08:07:14
【问题描述】:
我有 UITableviewCell,我在单元格中放置了 4 个按钮。当我单击一个按钮时,我需要将其背景颜色更改为红色。
现在我已经为此编写了代码,当我单击一个按钮时,该按钮的背景颜色不会改变,而不是其他行中的相同按钮改变背景颜色。
用例: 1.我在 UITableView 中有 10 行,每个单元格包含 4 个按钮,命名为 “好”、“更好”、“最好”、“最差”。
- 当我点击第一行的“良好”按钮时,我希望它的颜色会变为红色。
3.现在如果我点击第一行的“好”按钮,那么它在向下滚动时不会改变颜色,但我可以看到第 4 和第 8 的“好”按钮变为红色。
所以我的代码在改变颜色时出现了一些错误。
请检查我的表格视图代码和按钮点击代码
-(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 ];
}
请帮我解决这个问题
【问题讨论】:
标签: ios objective-c uitableview uibutton