ios 如何取消选择选定的 UITableView 单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3968037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:56:34  来源:igfitidea点击:

How to deselect a selected UITableView cell?

iosobjective-ciphoneuitableviewinterface-builder

提问by Ram

I am working on a project on which I have to preselect a particular cell.

我正在处理一个必须预先选择特定单元格的项目。

I can preselect a cell using -willDisplayCell, but I can't deselect it when the user clicks on any other cell.

我可以使用 预选一个单元格-willDisplayCell,但是当用户单击任何其他单元格时我无法取消选择它。

- (void)tableView:(UITableView*)tableView 
        willDisplayCell:(UITableViewCell*)cell
        forRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    AppDelegate_iPad *appDelegte = 
      (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];

    if ([appDelegte.indexPathDelegate row] == [indexPath row])
    {
        [cell setSelected:YES];    
    } 
}

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

    AppDelegate_iPad *appDelegte = 
      (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];

    NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
    appDelegte.indexPathDelegate = indexPath;
    [materialTable deselectRowAtIndexPath:indexpath1 animated:NO];
}

Can you help?

你能帮我吗?

回答by Saikiran K

use this code

使用此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    //Change the selected background view of the cell.
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
 }

Swift 3.0:

斯威夫特 3.0:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Change the selected background view of the cell.
    tableView.deselectRow(at: indexPath, animated: true)
}

回答by Shamitha

Use the following method in the table view delegate's didSelectRowAtIndexpath(Or from anywhere)

在表视图委托中使用以下方法didSelectRowAtIndexpath(或从任何地方)

[myTable deselectRowAtIndexPath:[myTable indexPathForSelectedRow] animated:YES];

回答by ikosdroid

Try this:

尝试这个:

for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

回答by jfalexvijay

Please check with the delegate method whether it is correct or not. For example;

请检查委托方法是否正确。例如;

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

for

为了

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

回答by Thomas Neuteboom

It might be useful to make an extension in Swift for this.

为此在 Swift 中进行扩展可能会很有用。

Swift 4 and Swift 5:

斯威夫特 4 和斯威夫特 5:

Swift extension (e.g. in a UITableViewExtension.swift file):

Swift 扩展(例如在 UITableViewExtension.swift 文件中):

import UIKit

extension UITableView {

    func deselectSelectedRow(animated: Bool)
    {
        if let indexPathForSelectedRow = self.indexPathForSelectedRow {
            self.deselectRow(at: indexPathForSelectedRow, animated: animated)
        }
    }

}

Use e.g.:

使用例如:

override func viewWillAppear(_ animated: Bool)
{
    super.viewWillAppear(animated)

    self.tableView.deselectSelectedRow(animated: true)
}

回答by Andrea.Ferrando

Swift 4:

斯威夫特 4:

tableView.deselectRow(at: indexPath, animated: true)

回答by tBug

This is a solution for Swift 4

这是 Swift 4 的解决方案

 in tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

just add

只需添加

tableView.deselectRow(at: indexPath, animated: true)

it works like a charm

它就像一个魅力

example:

例子:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
//some code
}

回答by Drew C

In addition to setting the cell as selected, you also need to inform the tableView that the cell is selected. Add a call to -tableView:selectRowAtIndexPath:animated:scrollPosition:to your willDisplayCell:method: and you will be able to deselect it as normal.

除了将单元格设置为选中状态之外,还需要通知tableView该单元格被选中。添加-tableView:selectRowAtIndexPath:animated:scrollPosition:对您的willDisplayCell:方法的调用:您将能够像往常一样取消选择它。

- (void)tableView:(UITableView*)tableView 
  willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    AppDelegate_iPad *appDelegte = (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];

    if ([appDelegte.indexPathDelegate row] == [indexPath row])
    {
        // SELECT CELL
        [cell setSelected:YES]; 
        // TELL TABLEVIEW THAT ROW IS SELECTED
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];   
    } 
}

Be sure to use UITableViewScrollPositionNone to avoid odd scrolling behavior.

一定要使用 UITableViewScrollPositionNone 来避免奇怪的滚动行为。

回答by Jordan

This should work:

这应该有效:

[tableView deselectRowAtIndexPath:indexpath1 animated:NO];

Just make sure materialTable and tableView are pointing to the same object.

只要确保 materialTable 和 tableView 指向同一个对象。

Is materials connected to the tableView in Interface Builder?

材质是否连接到 Interface Builder 中的 tableView?

回答by CHiP-love-NY

If you want to select any table cell with the first click and deselect with the second, you should use this code:

如果您想通过第一次单击选择任何表格单元格并在第二次单击时取消选择,您应该使用以下代码:

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

    if (self.selectedIndexPath && 
        [indexPath compare:self.selectedIndexPath] == NSOrderedSame) {

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
         self.selectedIndexPath = nil;
    } 
    else {
        self.selectedIndexPath = indexPath;
    }
}