在单击它时更改UITableViewCell中按钮的图像。

时间:2018-04-02 12:53:48

标签: ios objective-c uitableview

我正在开发一个应用程序,其中 UITableViewCell 中的 UIButton 的图像应该在点击上更改,我已经在自定义单元格。现在,我可以更改按钮的图像,但是当我向下滚动时也会改变其他几个按钮的图像(在滚动时调用cellForRowAtIndexPath:)。

这是代码。

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        _cell = (ListTableViewCell *)[self.tblList dequeueReusableCellWithIdentifier:@"listTableViewCell"];
        if (_cell == nil) {
            _cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"listTableViewCell"];
        } else {
            _cell.imgIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_arrImages objectAtIndex:indexPath.row]]];
            _cell.lblList.text = [NSString stringWithFormat:@"%@",[_arrNames objectAtIndex:indexPath.row]];
            _cell.btnList.tag = indexPath.row;
            if (_cell.btnList.tag == indexPath.row) {
                [_cell.btnList addTarget:self action:@selector(btnPressedMethodCall:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        return _cell;
    }


- (void) btnPressedMethodCall:(UIButton*)sender  {
    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"red_image.png"] forState:UIControlStateSelected];
        [sender setSelected:NO];
    } else {
        [sender setImage:[UIImage imageNamed:@"black_image.png"] forState:UIControlStateNormal];
        [sender setSelected:YES];
    }
}

有人可以告诉我这个问题是如何解决的。感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

而不是在按钮点击事件中更改图像。在button中添加选定的indexPath NSMutableArray,并在cellForRow方法检查NSMutableArray中包含当前indexPath。如果是,则更改button图像,否则设置如下图所示的正常图像。

<强>夫特

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tblVW.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    cell.selectionStyle = .none

    cell.btn.tag = indexPath.row
    cell.btn.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)

    if arrIndexPaths.contains(indexPath) {
        cell.btn.setImage(YOUR_BUTTON_SELECTED_IMAGE, for: .normal)
    }
    else {
        cell.btn.setImage(YOUR_BUTTON_DEFAULT_IMAGE, for: .normal)
    }
    cell.layoutSubviews()
    return cell;
}

@IBAction func btnTapped(_ sender: UIButton) {
    let selectedIndexPath = NSIndexPath.init(row: sender.tag, section: 0)
    arrIndexPaths.add(selectedIndexPath)
    self.tblVW.reloadData()
}

如果您只想重新加载单行而不是self.tblVW.reloadData()替换self.tblVW.reloadRows(at: [selectedIndexPath as IndexPath], with: UITableViewRowAnimation.none)

目标C

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil] objectAtIndex:0];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.btn.tag = indexPath.row
    [cell.btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

    if ([arrIndexPaths containsObject: indexPath]) {
        [cell.btn setImage:YOUR_BUTTON_SELECTED_IMAGE forState:UIControlStateNormal];
    }
    else {
        [cell.btn setImage:YOUR_BUTTON_DEFAULT_IMAGE forState:UIControlStateNormal];
    }

    [cell layoutSubviews];
    return cell;
}

-(IBAction)btnTapped:(UIButton *)sender {
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    [arrIndexPaths addObject:selectedIndexPath];
    [self.tblVW reloadData];  // Reload Whole TableView

    //OR
    NSArray* indexArray = [NSArray arrayWithObjects:selectedIndexPath, nil];
    [self.tblVW reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone]; // Reload Single Row
}