集合查看单元格仅在1个案例中返回Nil

时间:2016-09-30 05:19:32

标签: ios swift uicollectionview

我使用 collectionViewController 作为用户提交表单,有两个部分:

  1. 强制性
  2. 可选..
  3. 每个部分都有一个自定义类,其中包含相关数据,基于用户信息,有4个项目可供选择。在didSelectItemAtIndexPath上,用户突出显示最符合其条件的单元格。我有一个按钮,其目标是将用户选择的单元格上传到我的云后端。这些信息来自collectionView?.indexPathsForSelectedItems(),因为我创建了一系列单元格'用户已选择的indexPaths,每个部分可能只有一个选定的行。我已经完成了提交的每个组合,并且只有当用户提交第0项第0项和第1项第3项时,它才能为第一部分的选择注册正确的单元格。当我断开相关的行时,我能够获得用户在这种情况下选择的单元格信息,但是否则它会为我的collectionview单元格返回nil,并且不允许我拉出正确的单元格标记。

    //within my didSelectItemAtIndexPath
    //I am properly adding and removing indexPaths..can add the code in if needed
    selectItems = self.collectionView?.indexPathsForSelectedItems()//indexPath array
    
    //my button controlling the submission of data
    bottomView.anchorButton.addTarget(self, action: #selector(postItems), forControlEvents: .TouchUpInside)
    //target method
    

    我正在尝试将单元格标记传递到我的数据库中,并且每个部分都是唯一的

    func postItems() {
    
        let indexPath1 = selectItems
    
        var lTag: Int?
        var pTag: Int?
    
    
        if let indexPath = indexPath1 {
    
            //I have printed the selected indexPath to make sure I am passing the right values
            // when only the mandatory section is returned, there isnt a problem
            //returning the cell type that I want and the cell returns the value I want
            if indexPath.count == 1 {
                if indexPath[0].section == 0 {
    
                    if  let customCell = collectionView?.cellForItemAtIndexPath(indexPath[0])  {
                        let cell = customCell as! CustomCell1
                        lTag = cell.tag
    
                    }
    
                } else if indexPath[0].section == 1 {
                    if let customCell2 = collectionView?.cellForItemAtIndexPath(indexPath[0]) {
                        let cell2 = customCell2 as! CustomCell2
                        pTag = cell2.tag
                    }
                }
            }
    
            //the problem occurs when I have my optional section included
            if indexPath.count == 2 {
    
                //the problem occurs here..can not unwrap the cell for this indexPath (0-0)
                //but will do so when in a breakpoint..does not have a problem for
                other combinations (e.g. 0-0 and 1-0/1-1/1-2)
                if indexPath[0].section == 0 {
    
                    if let customCell = collectionView?.cellForItemAtIndexPath(indexPath[0])  {
                        let cell = customCell as! CustomCell
                        lTag = cell.tag
    
    
                    }
    
                } else if indexPath[0].section == 1 {
                    if let customCell2 = collectionView?.cellForItemAtIndexPath(indexPath[0]) {
                        let cell2 = customCell2 as! CustomCell2
                        pTag = cell2.tag
    
                    }
                }
    
    
                if indexPath[1].section == 0 {
    
                    if  let customCell = collectionView?.cellForItemAtIndexPath(indexPath[1])  {
                        let cell = customCell as! CustomCell
                        lTag = cell.tag
    
                    }
                }  else if indexPath[1].section == 1 {
                    if let customCell2 = collectionView?.cellForItemAtIndexPath(indexPath[1]) {
                        let cell2 = customCell2 as! CustomCell2
                        pTag = cell2.tag
    
    
                    }
                }
            }
        }
    
        if lTag != nil {
            print("Tag isn't nil")
    
        } else {
    
            print("Tag is nil because cell is nil value and could not be unwrapped")
        }
    }
    

    结尾仅用于测试是否已为问题标记分配值。我不确定是否有更好的方法来检查是否根据其部分选择了多个indexPath,但在我发现此错误之前,我认为这个代码可以做到。

    当选择0-00-0时,单元格是否仅在1-3返回nil是有原因的。有时,单元格会打开并取出我的自定义单元格的值,但大多数情况下它不会。“/ p>

0 个答案:

没有答案