為什么沒有調用-didDeselectRowAtIndexPath?

[英]Why is -didDeselectRowAtIndexPath not being called?


I created a fresh project (Xcode 4, Master-Detail application) just to see if I'm doing something wrong, but I still have the same problem. I want to call -reloadData when the user deselects a cell, so this is my code:

我創建了一個新項目(Xcode 4,Master-Detail應用程序),看看我做錯了什么,但我仍然遇到同樣的問題。我想在用戶取消選擇單元格時調用-reloadData,所以這是我的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView reloadData];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return indexPath;
}

The problem is that didDeselectRowAtIndexPath and willDeselectRowAtIndexPath don't seem to be called. Is this the expected behavior? The docs for tableView:didDeselectRowAtIndexPath: state

問題是似乎沒有調用didDeselectRowAtIndexPath和willDeselectRowAtIndexPath。這是預期的行為嗎? tableView的文檔:didDeselectRowAtIndexPath:state

Tells the delegate that the specified row is now deselected.

告訴代理現在取消選擇指定的行。

so I guess that it should work as I thought.

所以我猜它應該像我想的那樣工作。

8 个解决方案

#1


17  

the documentation of tableView:willDeselectRowAtIndexPath: also says that

tableView的文檔:willDeselectRowAtIndexPath:也就是說

This method is only called if there is an existing selection when the user tries to select a different row. The delegate is sent this method for the previously selected row. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down.

僅當用戶嘗試選擇其他行時存在現有選擇時,才會調用此方法。代理將為先前選定的行發送此方法。您可以使用UITableViewCellSelectionStyleNone在觸摸時禁用單元格高亮顯示的外觀。

It not worked for we when I used UITableViewCellSelectionStyleNone.

當我使用UITableViewCellSelectionStyleNone時,它不適用於我們。

#2


47  

If you call deselectRowAtIndexPath:animated:, the delegate methods tableView:willDeselectRowAtIndexPath: and tableView:didDeselectRowAtIndexPath: message are not sent.

如果調用deselectRowAtIndexPath:animated:,則不會發送委托方法tableView:willDeselectRowAtIndexPath:和tableView:didDeselectRowAtIndexPath:message。

#3


9  

One other quirk that I've found — in IOS 5.1, at any rate — is that if you call reloadData on the table, you won't get didDeselectRowAtIndexPath for any selected rows. In my case, I adjust the cell layout slightly depending on whether it's selected or not, so I needed to manually do that work prior to reloading the table data.

我發現的另一個怪癖 - 無論如何在IOS 5.1中 - 如果你在表上調用reloadData,你將不會獲得任何選定行的didDeselectRowAtIndexPath。在我的情況下,我根據是否選擇了單元格布局,因此我需要在重新加載表格數據之前手動完成這項工作。

#4


3  

I know this is an old question but I just ran into the same problem.

我知道這是一個老問題,但我遇到了同樣的問題。

If you use

如果你使用

[tableView reloadData]

Then The table data is reloaded and no rows are selected behind the scenes - meaning

然后重新加載表數據,並且在幕后沒有選擇任何行 - 意思是

only

只要

didSelectRowAtIndexPath

is ever called. I hope this helps someone who comes across this problem.

被稱為。我希望這可以幫助遇到這個問題的人。

#5


2  

A clean way of solving this problem is to do the following:

解決此問題的一種簡潔方法是執行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
    // Do your cell setup work here...

    //If your cell should be selected...
    if cellShouldBeSelected {
        tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
    }

    return cell
}

This solves this entire problem of a cell not responding to being deselected after a tableView.reloadData()call happens.

這解決了在tableView.reloadData()調用發生后,單元格沒有響應被取消選擇的整個問題。

#6


0  

Set allowsMultipleSelection for that tableview to TRUE

將該tableview的allowMultipleSelection設置為TRUE

  self.tableView.allowsMultipleSelection = YES;

#7


0  

I think it's just simple mistake! Why don't you use following:

我認為這只是一個簡單的錯誤!你為什么不使用以下內容:

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

Instead of using just "tableView" in that line?
I guess, and pretty sure that above line would give you the solution!
hope this helped you, if you looked back at your old question!!!
Kudos! :)

而不是只在該行中使用“tableView”?我想,並且非常確定上面的線會給你解決方案!希望這能幫助你,如果你回頭看看你的老問題!獎勵! :)

#8


0  

When any cell is selected the first time, the -[UITableViewDelegate tableView:didDeselectRowAtIndexPath:] method is not called, but the -[UITableViewDelegate tableView:didSelectRowAtIndexPath:]. Just after selecting one more cell, the didDeselectRowAtIndexPath is called right after the didSelectRowAtIndexPath.

當第一次選擇任何單元格時,不調用 - [UITableViewDelegate tableView:didDeselectRowAtIndexPath:]方法,而是調用 - [UITableViewDelegate tableView:didSelectRowAtIndexPath:]。在選擇了另一個單元格之后,在didSelectRowAtIndexPath之后立即調用didDeselectRowAtIndexPath。

This is OK.

還行吧。

But if you have to show a cell as selected at the begining, (e.q. using UITableViewCellAccessoryCheckmark), then, after selecting another cell you probably want the didDeselectRowAtIndexPath method being called the first time, to deselect the previous cell.

但是如果你必須在開始時顯示一個單元格(例如使用UITableViewCellAccessoryCheckmark),那么,在選擇另一個單元格后,您可能希望第一次調用didDeselectRowAtIndexPath方法,以取消選擇前一個單元格。

The solution!

解決方案!

You have to call the -[UITableView selectRowAtIndexPath:animated:scrollPosition:] in the -[UITableViewDataSource tableView:cellForRowAtIndexPath:] to notify that a wanted cell is already selected.

你必須在 - [UITableViewDataSource tableView:cellForRowAtIndexPath:]中調用 - [UITableView selectRowAtIndexPath:animated:scrollPosition:]來通知已經選擇了想要的單元格。

Objective-C

Objective-C的

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    // saving the current selected row
    SelectedRow = indexPath.row;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    // preventing selection style
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.textLabel.text = "Some text";

    if (indexPath.row == SelectedRow) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        // just wanted to make it selected, but it also can scroll to the selected position
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }

    return cell;
}

Swift 3.1

Swift 3.1

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)!
    cell.accessoryType = UITableViewCellAccessoryType.checkmark
    selectedRow = indexPath.row
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)!
    cell.accessoryType = UITableViewCellAccessoryType.none
}

// MARK: - UITableViewDataSource

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

    // preventing selection style
    cell.selectionStyle = UITableViewCellSelectionStyle.none

    cell.textLabel?.text = "some text"

    if (indexPath.row == selectedRow) {
        cell.accessoryType = UITableViewCellAccessoryType.checkmark
        // just wanted to make it selected, but it also can scroll to the selected position
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
    }

    return cell
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/03/22/72f8447c751e1b7d3dce2d4cd1fa8f22.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com