簡體   English   中英

在表格視圖上單擊按鈕以更改表格視圖單元格中的圖像

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

我有一個帶有一個按鈕的表格視圖單元格。 單擊單元格按鈕時,該按鈕具有一種檢查按鈕標簽是否等於indexPath.row 然后,我試圖更改圖像。 但它沒有顯示。

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

}

此處按鈕圖像未更改。 我需要的是,當我單擊任何需要更改特定單元格圖像的單元格時,其他單元格都應顯示未選中的圖像。 而且,無論何時單擊任何單元格,其圖像都會更改為所選圖像。 我需要獲取特定的單元格值,例如textlabel名稱,subtextlabel名稱。

我怎么能做到這一點。 這是什么問題?

更新 :

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

添加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"];
}

然后將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];
    }    
}

我讀了你的代碼。 我發現的是您在按鈕點擊方法中犯了一個錯誤。

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

    }

您放置的if條件始終返回true,因為首先將發件人的標記存儲到rowNum,然后將二者進行比較

條件始終為真,並且將執行以下代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM