OGeek|极客世界-中国程序员成长平台

标题: ios - 按钮单击表格 View 以更改表格 View 单元格中的图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:14
标题: ios - 按钮单击表格 View 以更改表格 View 单元格中的图像

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

- (UITableViewCell *)tableViewUITableView *)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 actionselector(ButtonClicked forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)ButtonClickedUIButton*)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"];
    }

}

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

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

更新:

    -(void)ButtonClickedUIButton*)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"];
    }
}



Best Answer-推荐答案


添加这两个tableView的委托(delegate):

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

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

然后将 ButtonClicked 更改为:

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

关于ios - 按钮单击表格 View 以更改表格 View 单元格中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730874/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4