簡體   English   中英

選擇tableView單元格時更改單元格上的按鈕圖像

[英]Change button image on a cell when a tableView Cell is selected

我有一個UITableView與從.xib加載的表視圖單元格。 其中有一個標簽文本和一個選中標記按鈕。 我想在選擇單元格時更改按鈕的選中標記圖像。 (允許多項選擇)。 從這些選定的帶有更改的選中標記圖像的單元格中,我正在執行一項操作。

誰能告訴我該怎么做?

提前致謝....

請按照以下步驟更改按鈕圖像。

單選

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    if (selectedIndex == indexPath.row) {
      // change image here...
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndex = indexPath.row;
    [tableView reloadData];
}

如果需要多項選擇,則可以維護一個數組以捕獲索引並執行如下所示的有需要的操作。

多項選擇

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   if ([selectedIndexArray containsObject:indexPath.row]) {
      // change image to check mark here...
   } else {
      // Change image to un-check mark
   }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([selectedIndexArray containsObject:indexPath.row]) {
       [selectedIndexArray removeObject:indexPath.row];
    } else {
       [selectedIndexArray addObject:indexPath.row];
    }
  [tableView reloadData];
}

您可以在模型類中具有一個isCheckMarked字段,然后將其放入數組中以供表視圖填充。 然后,您可以在tableView的didSelectRow委托中切換該標志,並可以基於isCheckMarked更改選中標記圖像

暫無
暫無

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

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