簡體   English   中英

同時選擇另一個表格單元格時如何取消選擇表格單元格?

[英]How to deselect tableview cell when selecting another tableview cell in the meantime?

我正在通過使用第一次自動選擇表格視圖單元格

  [cell setSelected:YES animated:YES];

它第一次以編程方式選擇單元格。 工作正常。 現在,當我同時選擇另一個單元格時,我想取消選擇該單元格。 我怎樣才能做到這一點。 我知道當我選擇一個單元格然后使用tableview委托方法

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

會被調用,當取消選擇單元格時,將調用“在索引路徑上取消選擇行”。

但是問題是我以編程方式選擇了單元格,當我選擇另一個單元格時委托了方法

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

不打。

我應該怎么做才能解決這個問題,或者解決這個問題的邏輯是什么?

代替直接在單元格上調用setSelected:animated: selectRowAtIndexPath:animated:scrollPosition:而是調用表視圖的selectRowAtIndexPath:animated:scrollPosition:方法來設置初始選擇。

您可以使用以下方法取消選擇行:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

選擇索引后,只需要存儲索引即可。 因此,您只需要檢查是否已選擇索引路徑,然后取消選擇它即可。

拿一個NSIndexPath對象:

NSIndexPath *currentSelectedPath;

更新您的didSelectRow方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(currentSelectedPath)
    {
        [tableView deselectRowAtIndexPath:currentSelectedPath animated:YES];
    }
    currentSelectedPath = indexPath;

 // Do your other stuff
}

暫無
暫無

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

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