【问题标题】:change image of button in custom cell更改自定义单元格中按钮的图像
【发布时间】:2015-09-30 08:47:21
【问题描述】:

我想更改自定义单元格上按钮的图像,给定代码在双击时更改图像。我想在单击时更改图像(选中和取消选中)

- (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;
        }
    }

【问题讨论】:

  • 考虑更详细地澄清问题。这将帮助 SO 用户帮助您。就目前而言,它包含一大段代码,没有 cmets 或解释

标签: ios objective-c iphone uitableview tableviewcell


【解决方案1】:

你需要做这样的事情

-(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设置为按钮标签

cell.selectButton.tag=indexPath.row;

并将选择器设置为按钮

[cell.selectButton addTarget:self action:@selector(selectUser:) forControlEvents:UIControlEventTouchUpInside];

【讨论】:

    【解决方案2】:

    在 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];
    }
    

    【讨论】:

    • 如何解决此错误 - 在“_strong id”类型的对象上找不到属性“标签”
    • 还是有问题,第一个单元格双击换图,remeing单元格工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多