2016-09-26 30 views
0

我想保留子視圖(在這種情況下UITextFields)仍然活動(通常的方式,即點擊textField和鍵盤出現),而父視圖是一個UITableViewCell的userInteractionEnabled設置假。subview userinteraction當superview用戶交互禁用swift

進一步解釋 - 1.有一個從xib初始化的UITableViewCell。

  1. 在這個單元格中,我有三個UITextFields和一個UIButton。
  2. 在某些情況下,我需要禁用單元交互(以避免didSelectRowAtIndexPath) - 這部分,我沒有任何問題。
  3. 在步驟3之後,單元內的所有子視圖也被禁用。
  4. 但我需要在textFields中輸入一些文本,然後點擊按鈕並對其執行一些操作。

我做了什麼至今: 1.用於userInteractionEnabled並將其設置爲false,細胞 -

myPersonalInfoExpandedCell.userInteractionEnabled = false 

2.兩個文本框firstNameText和lastNameText,我啓用的用戶交互,並將其設置firstNameText到becomeFirstResponder

(myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.userInteractionEnabled = true 
(myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).lastNameText.userInteractionEnabled = true 
(myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.becomeFirstResponder() 

問題是,對於文本框,在userInteractionEnabled不工作,我懷疑是因爲userInteractionEnabled對於假表ViewCell。 becomeFirstResponder()正在爲firstNameText工作,但我需要與單元格內的所有UI元素交互。

我們該怎麼做?請有人可以幫忙嗎?

回答

2

要禁用cellSelection,請實現willSelectRowAtIndexPath委託方法,併爲您不想選擇的單元格返回nil。你說你想在某些條件下禁用選擇,所以我假設你有要爲其禁用選擇的行的indexPaths。只需返回這些indexPath的零。請參閱以下代碼。

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for 
    return nil; 
else 
    return indexPath; 
} 

也這樣做,以防止突出顯示的單元格。

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

這樣做,你不必setUserInteractionEnabled:單元格中的NO。這時候你的電池接頭的didSelectRowAtIndexPath方法委託方法的方法將不會被調用,你的文本框仍然會啓用

編輯

以下是上面的代碼

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for 
    return nil; 
else 
    return indexPath; 
} 


cell.selectionStyle = .None 
+0

的迅速版感謝沙岩的回答!對於我不想選擇的單元格返回false?我沒有得到。你能舉個例子嗎? –

+0

@LohithKorupolu我編輯了我的答案。看看它。如果你仍然有問題,請隨時詢問 –

+0

它的工作原理,我可以編輯我的textFields,但在同一個視圖中有我的UIButton問題。當我點擊按鈕時,我收到一個異常 libC++ abi。dylib:以NSException類型的未捕獲異常終止 –