Ios 在UITableView中滚动后,UIButton颜色会发生变化
我有一个tableview,我正在xib中设置它的单元格。我有一个按钮在它,当我点击它,它改变按钮颜色从红色到绿色。当我滚动tableview并返回绿色按钮时,它会再次返回红色。这是我的密码:Ios 在UITableView中滚动后,UIButton颜色会发生变化,ios,xcode,uitableview,Ios,Xcode,Uitableview,我有一个tableview,我正在xib中设置它的单元格。我有一个按钮在它,当我点击它,它改变按钮颜色从红色到绿色。当我滚动tableview并返回绿色按钮时,它会再次返回红色。这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"P
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell.btnHatirlat addTarget:self action:@selector(remind:)
forControlEvents:UIControlEventTouchUpInside];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
return cell;
}
- (void) remind:(UIButton *)sender {
UIButton *btnTemp = (UIButton *)sender;
int iTag = btnTemp.tag;
// I want to change image of sender
[btnTemp setBackgroundColor:[UIColor greenColor]];
}
编辑:我已添加我的解决方案作为答案。我希望这会有所帮助。我会这样做:
Tableview在重用单元格时不知道如何“记住”按钮状态
我会创建一个NSMutableArray,它会保存一个BOOL(或任何其他)标志,无论按钮是“开”还是“关”。如果愿意,可以使用“off”初始化该数组,然后相应地将与indexPath.row对应的索引处的值更改为“on”。
读取cellForRowAtIndexPath中的值,并相应地更改颜色
如果有多个部分,请为每个部分创建一个数组,并让它为相应数量的单元格保留一个数组
如果该表视图由DB备份,请添加状态标志(开/关),然后保存它。我不知道这是否需要重新加载数据才能使一切按预期工作。您需要存储按下按钮的单元格索引 试试这个->
- (void)remind:(UIButton *)sender {
UIButton *btnTemp = (UIButton *)sender;
self.iTag = btnTemp.tag;
/*For many buttons
[btnTemp setSelected:![btnTemp isSelected]];
self.buttonStates[btnTemp.tag] = @([btnTemp isSelected);
*/
// I want to change image of sender
[btnTemp setBackgroundColor:[UIColor greenColor]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.iTag == indexPath.row) {
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
/*For many buttons
[cell.btnHatirlat setSelected:[self.buttonStates[indexPath.row] boolValue]];
if ([cell.btnHatirlat isSelected]) {
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
*/
[cell.btnHatirlat addTarget:self action:@selector(remind:)
forControlEvents:UIControlEventTouchUpInside];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
return cell;
}
祝你好运 这是我的解决方案,我想再次感谢@Bernard Grabowski和@MobiDevCom。我已保存单击按钮的节和行号,并检查是否单击了它
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString* strTemp = [NSString stringWithFormat: @"%d/%d", indexPath.section, indexPath.row ];
//To check clicked button at current section and row is clicked
for (int i = 0; i < buttonClickedArray.count; i++)
{
if([[self.buttonClickedArray objectAtIndex:i] isEqualToString:strTemp])
[cell.btnHatirlat setBackgroundColor:[UIColor greenColor]];
}
NSString *str = [[etkinlikArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell.btnHatirlat addTarget:self action:@selector(remind:)
forControlEvents:UIControlEventTouchUpInside ];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO];
}
return cell;
}
- (void) remind:(UIButton *)sender
{
//to get clicked button's indexpath
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableProgram indexPathForCell:clickedCell];
self.currentIndexPathStr = [NSString stringWithFormat: @"%d/%d", clickedButtonPath.section, clickedButtonPath.row];
[self.buttonClickedArray addObject:currentIndexPathStr];
[btnTemp setBackgroundColor:[UIColor greenColor]];
}
(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“ProgramCell”;
ProgramCell*单元格=(ProgramCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil)
{
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“ProgramCell”所有者:自选项:nil];
单元格=[nib objectAtIndex:0];
}
NSString*strTemp=[NSString stringWithFormat:@“%d/%d”,indexath.section,indexath.row];
//选中当前节的已单击按钮,并单击行
对于(int i=0;i
您可以使用NSNumber YES | NO创建NSMutableArray,并存储所有按钮的状态!这是我之前回答这个问题时提供的答案(见下文)。这是一个有用的答案,帮助我找到了解决方案,但它不适用于多个部分,按钮标签也不正常工作。真的谢谢。我已经在你的答案的帮助下建立了我的解决方案,但我必须改进它。谢谢