在我向下滚动的UICollectionViewCell GetCell方法中,我没有遇到任何问题。但是,当我向上滚动时,我得到一个对象引用空指针。我有点困惑,我理解错误信息。但是,获取错误消息的原因令人费解。我应该指出,我不熟悉iOS生态系统,也许我在错误的地方进行故障排除。错误消息如下图所示:
GetCell方法的代码
public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
UIView cell;
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.Tag = 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;
}
如果添加以下代码下面的代码
在
if row< questionDataModel[indexPath.Section].QuestionAnswers.Count)
{
我可以向上滚动没有问题,但现在CellDispleyEnded方法存在潜在问题,请参见下图:
public override void CellDisplayingEnded (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
UITableViewRowSwitch btnCompliant = (UITableViewRowSwitch)cell.ViewWithTag (4);
//btnCompliant.relatedViewObserver.Dispose ();
btnCompliant.relatedView.RemoveObserver ((NSObject)btnCompliant.relatedViewObserver, "contentSize");
btnCompliant.ValueChanged -= btnCompliant_Click;
btnCompliant.relatedView = null;
UITableViewRowButton btnSave = (UITableViewRowButton)cell.ViewWithTag (5);
btnSave.TouchUpInside -= btnSave_Click;
UITableViewRowButton btnPicture = (UITableViewRowButton)cell.ViewWithTag (7);
btnPicture.TouchUpInside -= btnPicture_Click;
UITableViewRowButton btnComment = (UITableViewRowButton)cell.ViewWithTag (6);
btnComment.TouchUpInside -= btnComment_Click;
}