2017-03-09 20 views
0

下圖中的UISwitches位於集合視圖中。不過,我目前面臨的問題在哪裏。如果我在頂部選擇了一個開關,例如在屋頂結構中,當我向下滾動另一個不在視圖中的開關時,請選擇NON-SABS APPROVED PRODUCT開關。UISwitch的UICollectionview單擊事件

我已按照以下步驟來確定如何點擊人類點擊事件。我使用控制檯查看選擇開關時的輸出。 結果顯示,在某些情況下,交換機所需的交換機僅作爲第三個事件觸發,另外兩個觸發的事件是不在用戶視圖中的交換機。

要嘗試解決此問題,我嘗試通過將負面點擊事件分配給交換機來解決問題。 這在目前沒有工作,見下文

代碼代碼爲開關事件

public void btnQuestionAnswer_Click(object sender, EventArgs e) 
      { 
       UITableViewRowSwitch btnQuestionAnswer = (UITableViewRowSwitch)sender; 


        if ((btnQuestionAnswer.section.HasValue) && (btnQuestionAnswer.row.HasValue)) 
        { 

         db_QuestionAnswer questionAnswer = questionDataModel[btnQuestionAnswer.section.Value].QuestionAnswers[btnQuestionAnswer.row.Value]; 

         //Console.Write(questionAnswer.Answer); 
         Console.WriteLine(questionAnswer.Answer); 

         if ((btnQuestionAnswer.On)) 
         { 
          if (questionDataModel[btnQuestionAnswer.section.Value].ComplianceIndicator) 
          { 
           foreach (db_QuestionAnswer QA in questionDataModel[btnQuestionAnswer.section.Value].QuestionAnswers) 
           { 
            QA.isTicked = false; 
           } 
          } 
          questionAnswer.isTicked = true; 
          // ((UICollectionView)btnQuestionAnswer.relatedView).ReloadData(); 
         } 
         else 
         { 
          questionAnswer.isTicked = false; 
         } 
        } 

        else 
        { 
         btnQuestionAnswer.On = !btnQuestionAnswer.On; 
        } 
        var element = count.ToString(); 

      } 

公衆覆蓋UICollectionViewCell GetCell(UICollectionView的CollectionView,NSIndexPath indexPath) { UIView的細胞;

   if (questionDataModel[indexPath.Section].ComplianceIndicator) 
       { 
        cell = collectionView.DequeueReusableCell (QuestionUICollectionViewDelegateDataSource.complianceQuestionCellId, indexPath); 
       } 
       else 
       { 
        cell = collectionView.DequeueReusableCell (QuestionUICollectionViewDelegateDataSource.questionCellId, indexPath); 
       } 

       int row = indexPath.Row; 

       UILabel lblQuestionAnswer = (UILabel)cell.ViewWithTag (1); 
       UITableViewRowSwitch btnQuestionAnswer = (UITableViewRowSwitch)cell.ViewWithTag (2); 



       btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click; 

       btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click; 

       btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click; 
       if (row < questionDataModel [indexPath.Section].QuestionAnswers.Count) 
       { 
        lblQuestionAnswer.Text = questionDataModel[indexPath.Section].QuestionAnswers[indexPath.Row].Answer; 

        btnQuestionAnswer.section = indexPath.Section; 
        btnQuestionAnswer.row = indexPath.Row; 


        btnQuestionAnswer.On = questionDataModel [indexPath.Section].QuestionAnswers [indexPath.Row].isTicked; 

        //----------------TODO----------------// 
        // ---- 
        btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click; 

        btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click; 
        //if (!btnQuestionAnswer.hasEvent) 
        { 
         btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click; 
         //btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click; 
         btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click; 

         //btnQuestionAnswer.hasEvent = true; 
        } 

        btnQuestionAnswer.relatedView = collectionView; 
        if (questionDataModel [indexPath.Section].isLocked) 
        { 
         btnQuestionAnswer.Enabled = false; 
        } 
        else 
        { 
         btnQuestionAnswer.Enabled = true; 
        } 

        lblQuestionAnswer.Hidden = false; 
        btnQuestionAnswer.Hidden = false; 
       } 
       else 
       { 
        lblQuestionAnswer.Hidden = true; 
        btnQuestionAnswer.Hidden = true; 
       } 

       if (controller.loggedInUser.UserType != "Inspector") 
       { 
        btnQuestionAnswer.Enabled = false; 
       } 

       return (UICollectionViewCell)cell; 
      } 

enter image description here

回答

1

你最有可能需要重寫UICollectionViewCell以下方法:

public override void PrepareForReuse() 
    { 
     base.PrepareForReuse(); 
    } 

    public override void AwakeFromNib() 
    { 
     base.AwakeFromNib(); 

    } 

你看到的問題是,收集的看法是正確的「重用」細胞的已經加載到內存中,因此它保持單元格UIView內控件的當前「狀態」。因此,在準備重用時,您需要將所有UISwitch重置爲其默認值。然後,您可以使用'AwakeFromNib'覆蓋來確保您希望保持其開關狀態的單元格被相應地設置。在UIcollectionViewCell中保留交換機的當前狀態,然後在'AwakeFromNib'中應用它們,你可能會明智地獲得一些bool值。希望這可以幫助。

編輯:

因爲它似乎使用的是靜態的細胞,此刻,這裏是xamarins文檔的鏈接從基站UICollectionViewCell派生自己的自定義單元格。 Link

這應該給你你需要派生類,並給你訪問我上面提到的方法的信息,讓您在什麼出現在什麼狀態等等

+0

我如何添加更多的控制PrepareForReuse我得到以下錯誤沒有找到合適的錯誤發現修改 – George

+0

您是否正在爲您的單元格使用自定義類?或者正在使用靜態單元? E.g.公共部分類MyDerivedCell:UICollectionViewCell – Digitalsa1nt

+0

class MilestoneViewDelegateDataSource:UITableViewSource {}和class QuestionUICollectionViewDelegateDataSource:UICollectionViewSource {} – George