更改自定义单元格中按钮的图像 [英] change image of button in custom cell
本文介绍了更改自定义单元格中按钮的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在自定义单元格上更改按钮的图像,给定代码在双击时更改图像.我想在单击时更改图像(选中和取消选中)
I want to change the image of button on custom cell,given code changing the image on double click.I want to change image on single click(check and uncheck)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *localArrayData = [LocalArray objectAtIndex:indexPath.row];
cell.nameLabel.text =localArrayData;
cell.CheckButton.tag=indexPath.row;
[cell.CheckButton setImage:[UIImage imageNamed:@"checkU.png"] forState:UIControlStateNormal];
[cell.CheckButton addTarget:self action:@selector(CheckButtonAction:)forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)CheckButtonAction:(id)sender
{
NSInteger row = [sender tag];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
SimpleTableCell *cell = [DataTable cellForRowAtIndexPath:indexPath];
if(appDelegate.didSelect)
{
[cell.CheckButton setImage:[UIImage imageNamed:@"checkU.png"] forState:UIControlStateNormal];
appDelegate.didSelect = FALSE;
}
else
{
[cell.CheckButton setImage:[UIImage imageNamed:@"checkS.png"] forState:UIControlStateNormal];
appDelegate.didSelect = TRUE;
}
}
推荐答案
您需要执行以下操作
-(void)selectUser:(id)sender
{
UIButton *button=(UIButton *) sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:button.tag inSection:0];
CustomTableViewCell *tappedCell = (CustomTableViewCell *)[yourTableView cellForRowAtIndexPath:indexpath];
if ([tappedCell.selectButton.imageView.image isEqual:[UIImage imageNamed:@"green.png"]])
{
[tappedCell.selectButton setImage:[UIImage imageNamed:@"grey.png"] forState:UIControlStateNormal];
}
else
{
[tappedCell.selectButton setImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
}
}
在cellForRowAtIndexPath
方法中,您将单元格的indexPath设置为按钮标记
In cellForRowAtIndexPath
method, you will set cell's indexPath as button tag
cell.selectButton.tag=indexPath.row;
并将选择器设置为按钮
[cell.selectButton addTarget:self action:@selector(selectUser:) forControlEvents:UIControlEventTouchUpInside];
这篇关于更改自定义单元格中按钮的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文