2013-01-09 28 views
2

通常我的典型didSelectRowAtIndexPath方法是这样的:细胞得到未选择不取消命令

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 
    BGUIBusinessCellForDisplay * cellBiz = (BGUIBusinessCellForDisplay *) cell; 
    [BGDetailBusinessViewController detailButtonPressed:cellBiz.biz withNavController:self.navigationController]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

因此,如果一个行selectec,打开一个新的窗口或东西。然后快速取消选择该行。不取消选择行意味着当我们回到先前选择的行将仍然存在。

但是如果我检查我的过去的代码,我看到该行成功地取消,即使我没有打电话取消所有

我放个陷阱,到处跟踪,其中该行被取消当:

-(void)viewDidAppear:(BOOL)animated 
{ 

... 

PO(self.tableView.indexPathForSelectedRow); //always show null here 
while(false); 

}

通过viewDidAppear事实证明,说以后我从细节返回时,self.tablevView.indexPathForSelectedRow已经是空。所以该行已被取消选择,但何时何地?

我把更多的陷阱在明显的地方那些

-(void)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    while (false);//breakpoint here never called 
} 

-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    while (false); //breakpoint here never called 
} 

都不是以往任何时候都调用。

我放弃了。

现在不行biggy但这个困扰我没有尽头

这是一个完整didSelectRowForIndexPath这样你就可以验证有没有desellect命令有

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if ([cell isKindOfClass:[BGUIBusinessCellForDisplay class]]) 
    { 
     BGUIBusinessCellForDisplay * cellBiz = (BGUIBusinessCellForDisplay *) cell; 
     [BGDetailBusinessViewController detailButtonPressed:cellBiz.biz withNavController:self.navigationController]; 
    } 
    else if ([cell isKindOfClass:[BGAddMoreBusiness class]]) 
    { 
     BGBusinessEditViewController * editBusiness = [[BGBusinessEditViewController alloc]init]; 
     [self.navigationController SafelyPushController:editBusiness]; 
    } 
    PO(self.tableView.indexPathForSelectedRow); //not null here 
} 

回答

3

这是UITableViewController默认行为,请参阅文档clearsSelectionOnViewWillAppear

此属性的默认值为YES。当YES时,表格视图 控制器在收到消息时清除表的当前选择。将此属性设置为NO会保留 选择。

+0

我使用tableview与UIViewController不是UITableViewController。 –

+0

+1。看起来我已经知道发生了什么事情。 –

1

更新:通过viewWillAppear行仍被选中。通过viewDidAppear,它不再是。他们之间有[table reload]。看起来像[table reload]取消选择该行。

+0

是的,'reloadData'清除选择。 –