美文网首页iOS 进阶
tableView 中 deselectRowAtIndexPa

tableView 中 deselectRowAtIndexPa

作者: YHWXQ简简单单的生活 | 来源:发表于2016-09-19 10:12 被阅读1318次

效果图:添加了deselectRowAtIndexPath - 取消cell的选中状态

Paste_Image.png
效果图:没有添加deselectRowAtIndexPath Paste_Image.png
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
     // 不加此句时,在二级栏目点击返回时,此行会由选中状态慢慢变成非选中状态。
     // 加上此句,返回时直接就是非选中状态。
 }

问题1:有没有遇到过,导航+UITableView,在push,back回来之后,当前cell仍然是选中的状态

解决方法:添加一句[tableView deselectRowAtIndexPath:indexPath animated:YES]即可

问题2: 令人纠结的是,在没加这句的时候,有的视图同样回来之后,选中状态消失,为什么会出现这种情况呢?

 原来是,如果UITableView是在UITableViewController中时,就会默然取消,而如果是在UIViewController时,需要添加这一句,不过有时即使前者也需要添加,那是因为在视图加载时有其它功能代码,具体情况各异。所以后者必须加,前者可能需要加。

** 当然如果要求高的话,另外一种更加理想的办法是:**

- (void) viewWillAppear: (BOOL)inAnimated {
NSIndexPath *selected = [self.table indexpathForSelectedRow];i
f(selected) [self.table deselectRowAtIndexpath:selected animated:NO];
}      
 这种方法是在放回的过程中逐渐取消选中状态的,可以提示刚才点进去的是哪一行,默认的也正是这种效果。

相关文章

网友评论

    本文标题:tableView 中 deselectRowAtIndexPa

    本文链接:https://www.haomeiwen.com/subject/znngettx.html