setSelected:(BOOL)selected执行多次的问题
呐 就是自定义Cell的时候 实现文件会有这个
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {}
我在这个里面写了
CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
OneCarDetailVC.CarID = CarIDLabel.text;
[[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];
结果我发现在iPhone模拟器上一般不会出现什么奇怪的现象
但是在iPad上 几乎百分百要出现NavigationController
在点击cell后 快速Push两次同一个OneCarDetailVC
然后就会有奇奇怪怪的BAD_EXC_ACCESS
开NSZombie后Log出的是
[CarDetailTableViewController respondsToSelector:]: message sent to deallocated instance 0x7ff794556400
然后Stackoverflow上这个说明了问题...
Since cells are reused when you scroll through a large table view, the table view has to keep the list of selected cells separate. Not only that, but whenever it reuses a cell it has to set its selected property, because it may be using an old, invalid selected state from a previous incarnation.
所以说 就是从:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected == YES) {
CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
OneCarDetailVC.CarID = CarIDLabel.text;
[[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];
}
}
变为
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//多加一行这个
if (self.selected == selected) return;
[super setSelected:selected animated:animated];
if (selected == YES) {
CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
OneCarDetailVC.CarID = CarIDLabel.text;
[[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];
}
}