我想更改自定义单元格上的按钮图像,给定代码在双击时更改图像。我想单击更改图像(选中并取消选中)
- (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;
}
}
答案 0 :(得分:1)
在UIButton Action中写下这个
UIButton *btn = (UIButton *)sender;
if (btn.tag==0)
{
btn.tag = 1;
[btn setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
}
else
{
btn.tag=0;
[btn setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
答案 1 :(得分:1)
你需要做这样的事情
$csv = new csv_writer();
$csv->dataArray = array( 'value1', 'value2' );
$csv->filename = 'test';
$csv->csvHeader = array( 'value1', 'value2' );
$csv->create_csv();
在-(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];
}
}
方法中,您将单元格的indexPath设置为按钮标记
cellForRowAtIndexPath
并将选择器设置为按钮
cell.selectButton.tag=indexPath.row;