簡體   English   中英

didDeselectRowAtIndexPath未被調用

[英]didDeselectRowAtIndexPath isn't called

我在UITableView實現了didDeselectRowAtIndexPath方法,其中包括多個選擇。

出於某種原因, didDeselectRowAtIndexPath不會調用didDeselectRowAtIndexPath 有什么建議么? (我沒有意外拼錯didSelectRowAtIndexPath )。

謝謝!

有關細胞選擇的一些注意事項:

  • didDeselectRowAtIndexPath僅在單選UITableView調用,如果用戶單擊與已選擇的單元格不同的單元格。
  • 調用didSelectRowAtIndexPath來選擇新行,調用didDeselectRowAtIndexPath來取消選擇前一行
  • 在具有多選的UITableView ,未取消選擇上一行,因此在didSelectRowAtIndexPath處理取消選擇

你可以使用你實現的委托方法獲取單元格,並在檢查它的選擇后修改它,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // ... Uncheck
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

我目前使用帶有多個選擇的tableView,並且didDeselectRowAtIndexPath方法正在調用我(不同於上面答案中的第三個項目符號中所述)。

我注意到“預選”的單元格沒有正確調用didDeselectRowAtIndexPath,除非我做了三件事:

  1. 在cellForRowAtIndexPath中,當我找到一個我想要預選的單元格時(您可能會檢查匹配的數組條目,或者像我一樣檢查字符串),為該單元格設置cell.selected = true
  2. 在cellForRowAtIndexPath中,還要調用方法selectRowAtIndexPath以使單元格預選
  3. 在Interface Builder中,取消選中tableView的“在觸摸時顯示選擇”復選標記。 奇怪的是,這需要確保在點擊預選項目(即應該取消選擇)時,正確調用了didDeselectRowAtIndexPath。 在此輸入圖像描述

示例代碼:

if productsReceived!.rangeOfString(cell.productName.text!) != nil {
    cell.accessoryType = .Checkmark 
    tableView.selectRowAtIndexPath(indexPath, animated: true,   scrollPosition: UITableViewScrollPosition.None)
    cell.selected = true
}

我希望這可以幫助那些嘗試使用預選單元格實現多重選擇的人。

如果我沒弄錯的話, UITableView沒有用於取消選擇單元格的內置方法。 我相信你需要調用deselectRowAtIndexPath:以便調用didDeselectRowAtIndexPath

暫無
暫無

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

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