ios 当用户返回到视图控制器时如何取消选择 uitableview 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14956823/
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 22:21:39  来源:igfitidea点击:

How to unselect a uitableview cell when the user returns to the view controller

iphoneiosobjective-cuitableview

提问by hanumanDev

I have a uitableview cell highlighted when a user selects it and then is pushed to a detail view. I would like the cell to be unhighlighted when they return to the table view view controller.

当用户选择它然后被推送到详细信息视图时,我有一个 uitableview 单元格突出显示。我希望单元格在返回到表视图视图控制器时不突出显示。

how would I go about accomplishing this?

我将如何实现这一目标?

I'm guessing [cell.textLabel setHighlighted:NO];in viewWillAppear, but cell is undeclared if I put it there.

我猜 [cell.textLabel setHighlighted:NO];在 viewWillAppear 中,但如果我把它放在那里,单元格是未声明的。

thanks for any help

谢谢你的帮助

回答by iiFreeman

If you using UITableViewControllersubclass then just set property

如果您使用UITableViewController子类,则只需设置属性

self.clearsSelectionOnViewWillAppear = YES;

else on viewDidAppear just call

其他 viewDidAppear 只需调用

NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if (indexPath) {
    [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
}

// MARK: - Swift 3
if let indexPath = tableView.indexPathForSelectedRow {
    tableView.deselectRow(at: indexPath, animated: true)
}

回答by Dustin Spengler

Swift 3.0

斯威夫特 3.0

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

    if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
        self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
    }
}

回答by Girish

try

尝试

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Deselect the row which was tapped
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

回答by Alexander of Norway

I decided to make this an answer instead of a comment.

我决定将此作为答案而不是评论。

If you are using storyboard: Click on your UITableViewController -> Check the "Selection: Clear on Appearance" box

如果您使用的是故事板:单击您的 UITableViewController -> 选中“选择:清除外观”框

回答by neoneye

swift 3

迅捷 3

func clearOnAppearance() {
    for indexPath in tableView.indexPathsForSelectedRows ?? [] {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

回答by alex

Or some inelegant way =)

或者一些不雅的方式 =)

NSArray *arrayWithPaths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *path in arrayWithPaths)
{
    [tableview deselectRowAtIndexPath:path animated:NO];
}