简体   繁体   English

如何在iphone中再次显示表格视图时删除单元格选择样式颜色?

[英]How to remove the cell selection style color when once again showing the table view in iphone?

I am new to iphone development.I created a table displaying my contents.If i select a row ,it state is highlighted in blue color and navigates to another view and if i click the back button it navigates back to the table showing the clicked cell in blue color,i want to remove the highlighted color on table while navigating back to its view.How can i do that. 我是iphone开发的新手。我创建了一个显示我的内容的表。如果我选​​择一行,它的状态会以蓝色突出显示并导航到另一个视图,如果我单击后退按钮,它会导航回显示单击的单元格的表格。在蓝色的颜色,我想在导航回到它的视图时删除桌子上突出显示的颜色。我怎么能这样做。 Thanks. 谢谢。

I think the generally accepted way to do this is to deselect the cell as you're navigating to the new view. 我认为通常接受的方法是在导航到新视图时取消选择单元格。 Instead of viewWillAppear, use the tableview delegate method didSelectRowAtIndexPath and the same deselectRowAtIndexPath you were using. 而不是viewWillAppear,使用tableview委托方法didSelectRowAtIndexPath和您使用的相同deselectRowAtIndexPath。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
  [tableView deselectRowAtIndexPath:newIndexPath animated:YES];
}

(and by generally accepted, I mean "stuff I most often see in example code". It depends on what you want it to look like in the end) (并且通过普遍接受,我的意思是“我经常在示例代码中看到的东西”。这取决于你最终希望它看起来像什么)

You are doing it wrong, on cellForRowAtIndexPath method. 你在cellForRowAtIndexPath方法上做错了。 use this 用这个

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

On your UITableViewCells 在你的UITableViewCells上

This would be the correct way if you wanted the cell not to be highlighted nor on selection touch neither on coming back to view. 如果您希望单元格不会突出显示,也不会在选择触摸时返回查看,这将是正确的方法。 If you only want the cell deselected when coming back to view the other solutions are more suitable. 如果您只想在返回时取消选择单元格,则其他解决方案更合适。

I finally got it by implementing this in my table view class. 我终于通过在我的表视图类中实现它来实现它。

- (void)viewWillAppear:(BOOL)animated  
{  
    NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];  
    [self.tableView deselectRowAtIndexPath:tableSelection animated:NO];  
}  

The default implementation of viewDidAppear: should take care of that for you. viewDidAppear:的默认实现viewDidAppear:应该为您处理。 If you did override that method in your table view controller, don't forget to call [super viewDidAppear:animated] in your override method. 如果你在表视图控制器中覆盖了该方法,请不要忘记在override方法中调用[super viewDidAppear:animated]

For Swift 3: 对于Swift 3:

override func viewWillDisappear(_ animated: Bool) {
        if let indexPath = self.tableview.indexPathForSelectedRow {
            tableview.deselectRow(at: indexPath, animated: true)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM