菜鸟教程小白 发表于 2022-12-11 22:14:51

ios - 按钮单击表格 View 以更改表格 View 单元格中的图像


                                            <p><p>我有一个带有一个按钮的表格 View 单元格。单击单元格按钮时,该按钮具有一种检查 if 按钮标记是否等于 <code>indexPath.row</code> 的方法。然后我正在尝试更改图像。但它没有显示。</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @&#34;FilterTableViewCell&#34;;
    FilterTableViewCell *cell = ;
    cell.cellBtn.tag = indexPath.row;
    cell.radioImage.tag = indexPath.row;
    ;
    return cell;
}

-(void)ButtonClicked:(UIButton*)sender
{
   int rowNum = [(UIButton*)sender tag];
      NSIndexPath* indexPath = ;
    FilterTableViewCell *cell = ;
    if (sender.tag == rowNum) {
      cell.radioImage.image = ;
    } else {
      cell.radioImage.image = ;
    }

}
</code></pre>

<p>这里的按钮图像没有改变。我需要的是,当我单击特定单元格图像需要更改的任何单元格时,其他单元格应该显示未选择的图像。每当我单击任何图像更改为选定图像的单元格时。我需要获取特定的单元格值,例如 textlabel 名称、subtextlabel 名称。</p>

<p>我怎样才能做到这一点。这里有什么问题?</p>

<p>更新:</p>

<pre><code>    -(void)ButtonClicked:(UIButton*)sender
    {
       int rowNum = [(UIButton*)sender tag];
FilterTableViewCell *cell = ];
if (sender.tag != _currentSelectedIndex) {
      cell.radioImage.image = ;
    } else {
      cell.radioImage.image = ;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>添加这两个tableView的委托(delegate):</p>

<pre><code>- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterTableViewCell *cell = ;
    cell.radioImage.image = ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterTableViewCell *cell = ;
    cell.radioImage.image = ;
}
</code></pre>

<p>然后将 <code>ButtonClicked</code> 更改为:</p>

<pre><code>-(void)ButtonClicked:(UIButton*)sender
{
    int rowNum = [(UIButton*)sender tag];
    NSIndexPath* indexPath = ;
    if (sender.tag == rowNum) {
      ;
    }   
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 按钮单击表格 View 以更改表格 View 单元格中的图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54730874/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54730874/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 按钮单击表格 View 以更改表格 View 单元格中的图像