简体   繁体   English

在表格视图上单击按钮以更改表格视图单元格中的图像

[英]Button click on table view to change the image in my table view cell

I have one table view cell with one button. 我有一个带有一个按钮的表格视图单元格。 On click of the cell button which has one method to check the if button tag equals to indexPath.row . 单击单元格按钮时,该按钮具有一种检查按钮标签是否等于indexPath.row Then I am trying to change the image. 然后,我试图更改图像。 But its not showing. 但它没有显示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"FilterTableViewCell";
    FilterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.cellBtn.tag = indexPath.row;
    cell.radioImage.tag = indexPath.row;
    [cell.cellBtn addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)ButtonClicked:(UIButton*)sender
{
     int rowNum = [(UIButton*)sender tag];
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
    FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    if (sender.tag == rowNum) {
        cell.radioImage.image = [UIImage imageNamed:@"selected.png"];
    } else {
        cell.radioImage.image = [UIImage imageNamed:@"unselected.png"];
    }

}

Here button image is not changing. 此处按钮图像未更改。 What I need is, when ever I click on any cell that particular cell image needs to change and other cells should show unselected image. 我需要的是,当我单击任何需要更改特定单元格图像的单元格时,其他单元格都应显示未选中的图像。 And when ever I click on any cell which image is changed as selected image. 而且,无论何时单击任何单元格,其图像都会更改为所选图像。 I needs to get that particular cell values like textlabel name, subtextlabel name. 我需要获取特定的单元格值,例如textlabel名称,subtextlabel名称。

How can i achive that. 我怎么能做到这一点。 What's the problem here ? 这是什么问题?

Updated : 更新 :

    -(void)ButtonClicked:(UIButton*)sender
    {
       int rowNum = [(UIButton*)sender tag];
FilterTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowNum inSection:0]];
 if (sender.tag != _currentSelectedIndex) {
        cell.radioImage.image = [UIImage imageNamed:@"selected.png"];
    } else {
        cell.radioImage.image = [UIImage imageNamed:@"unselected.png"];
    }
}

add these two delegates of tableView: 添加tableView的这两个委托:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    cell.radioImage.image = [UIImage imageNamed:@"unselected.png"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    cell.radioImage.image = [UIImage imageNamed:@"selected.png"];
}

and then change ButtonClicked to: 然后将ButtonClicked更改为:

-(void)ButtonClicked:(UIButton*)sender
{
    int rowNum = [(UIButton*)sender tag];
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
    if (sender.tag == rowNum) {
        [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }    
}

I read your code. 我读了你的代码。 What i found is that you made a mistake in the button tapped method. 我发现的是您在按钮点击方法中犯了一个错误。

    -(void)ButtonClicked:(UIButton*)sender
    {
         int rowNum = [(UIButton*)sender tag];
            NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_currentSelectedIndex inSection:0];
        FilterTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        if (sender.tag == rowNum) {
    cell.radioImage.image = [UIImage imageNamed:@"selected.png"];
        } else {
            cell.radioImage.image = [UIImage imageNamed:@"unselected.png"];
        }

    }

The if condition which you have placed is always return true because first you store the sender's tag to rowNum and then you compare both the two . 您放置的if条件始终返回true,因为首先将发件人的标记存储到rowNum,然后将二者进行比较

CONDITION ALWAYS BE TRUE AND FOLLOWING CODE WILL BE EXECUTED 条件始终为真,并且将执行以下代码

    cell.radioImage.image = [UIImage imageNamed:@"selected.png"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM