2012-08-23 40 views
2

我有一個應用程序關閉並在觸摸UITableViewCell時前往Safari打開URL。當我回到應用程序時,單元格仍然被選中幾秒鐘。爲什麼不立即取消選擇?這是一個錯誤嗎?下面是代碼:爲什麼UITableCell不能在Safari中打開URL時立即取消選擇?

我試圖移動[tableView deselectRowAtIndexPath:indexPath animated:NO];頂端和關閉動畫,但它並沒有幫助。這不是什麼大不了的事情,但我希望它儘可能立即取消。

這也與UIButton發生。回到應用程序後,它會在突出顯示的狀態下保持一兩秒鐘。

+0

你可以發佈更多的代碼,請 – LostPuppy

回答

5

[tableView deselectRowAtIndexPath:indexPath animated:NO];這樣的更改在通過運行循環的下一次迭代中生效。當您通過openURL:退出時,會延遲下一次迭代,直到您切換回應用。切換回來是通過在屏幕的圖像中循環,然後稍後離開,然後再次使應用程序交互。因此選定的圖像仍然存在。

暫且不論實現的細節,邏輯是,影響畫面內容的東西被捆綁在一起,並提出原子,這樣,當你在做視圖調整你不必須不斷想「哦,不,如果什麼框架現在重新繪製,只有到這裏的變化完成了?'。根據iOS多任務模型,調整界面的原子單位不會發生,直到您重新進入應用程序。

快速修復:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // deselect right here, right now 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    if (indexPath.section == 0 && indexPath.row == 0) { 
     [[UIApplication sharedApplication] 
        performSelector:@selector(openURL:) 
        withObject:[NSURL URLWithString:@"http://www.example.com/"] 
        afterDelay:0.0]; 

     /* 
       performSelector:withObject:afterDelay: schedules a particular 
       operation to happen in the future. A delay of 0.0 means that it'll 
       be added to the run loop's list to occur as soon as possible. 

       However, it'll occur after any currently scheduled UI updates 
       (such as the net effect of a deselectRowAtIndexPath:...) 
       because that stuff is already in the queue. 
     */ 
    } 

} 
+0

哇,這真是一個有見地的解釋和一個很好的解決方案。謝謝! – woz

相關問題